Regular expression,

hi

errorMsg=“Subsequent deposit instructions not found or
incomplete.Initial deposit instructions not found or incomplete.Analysis
complete for policy (SS13323496).Analysis complete for policy
(SS13323496), coverage (01).Premium calculation restricted on coverage
(01). Rates not
re-calculated.”

puts errorMsg[/Analysis complete for policy (.?), coverage (.?)/]

the above line give the output

Analysis complete for policy (SS13323496).Analysis complete for policy
(SS13323496), coverage (01)

My question here is, the above one should give the output
“Analysis complete for policy (SS13323496), coverage (01)”

why it includes “Analysis complete for policy (SS13323496)” in the
front?could anyone please clarify me?

On Fri, Dec 27, 2013 at 6:47 AM, Raja gopalan [email protected]
wrote:

puts errorMsg[/Analysis complete for policy (.?), coverage (.?)/]
why it includes “Analysis complete for policy (SS13323496)” in the
front?could anyone please clarify me?

Because that is the shortest sequence that will match. Note that
matching always goes from left to right and there is no mechanism
which would truncate the beginning of the sequence if there would be a
shorter match. Reluctance does not go that far.

You need a more precise match, e.g. by excluding brackets from the inner
parts:

irb(main):009:0> errorMsg=“Subsequent deposit instructions not found
or incomplete.Initial deposit instructions not found or
incomplete.Analysis complete for policy (SS13323496).Analysis complete
for policy (SS13323496), coverage (01).Premium calculation restricted
on coverage (01). Rates not re-calculated.”
=> “Subsequent deposit instructions not found or incomplete.Initial
deposit instructions not found or incomplete.Analysis complete for
policy (SS13323496).Analysis complete for policy (SS13323496),
coverage (01).Premium calculation restricted on coverage (01). Rates
not re-calculated.”
irb(main):010:0> errorMsg[/Analysis complete for policy ([^()]?),
coverage ([^()]
?)/]
=> “Analysis complete for policy (SS13323496), coverage (01)”

Cheers

robert

hi Robert K.

That’s an excellent way, thank you so much.

RAJ

hi Robert

Please consider another condition here,

Analysis complete for policy (SS14807031).
Analysis complete for policy (SS14807031), coverage (05).
Analysis complete for policy (SS14807031), coverage (02).
Analysis complete for policy (SS14807031), coverage (01).

If I use this,
errorMsg[/Analysis complete for policy ([^()]?), coverage
([^()]
?)/]

Then it’s matching with the second comparison,(i.e) Analysis complete
for policy (SS14807031), coverage (05).but I want to thrid one which
ends with (02), Now I will have the number 02 from the program, So how
would I insert this number inside our regular expression like

a=coveragenumber #02 or 05 or 06

I know the below code is wrong because I have directly given #{a}inside
regular expression when this would work only for string.
errorMsg[/Analysis complete for policy ([^()]*?), coverage (#{a})/]

Could you please suggest me how would I do that?

RAJ

hi Michael Hansen

It works.I was not aware of this conversion. Thank you so much.

RAJ

encoding: UTF-8

rubocop offences
62

“C:\Program Files (x86)\Lotus\Notes\notes.exe” "=C:\Program Files
(x86)\Lotus\Notes\notes.ini"From: Raja gopalan
[email protected]

for policy (SS14807031), coverage (05).but I want to thrid one which

RAJ


Posted via http://www.ruby-forum.com/.

Here is a first draft of a solution, make the string then the Regexp.

a = ‘02’

regex_string = ‘Analysis complete for policy ([^()]*?), coverage (’ +
a

  • ‘)’
    regex = Regexp.new regex_string

I used this format because using double quotes and interpolation on the
whole string replaced the ( and ) with ( and ), then the regex used
them
as grouping symbols.

Michael

Michael Hansen

On Mon, Jan 6, 2014 at 2:52 PM, Raja gopalan [email protected]
wrote:

hi Michael Hansen

It works.I was not aware of this conversion. Thank you so much.

You can do it simpler and use string interpolation inside the regexp:

$ ruby x.rb
“Analysis complete for policy (SS14807031), coverage (02)”
$ cat x.rb
s = <<TEXT
Analysis complete for policy (SS14807031).
Analysis complete for policy (SS14807031), coverage (05).
Analysis complete for policy (SS14807031), coverage (02).
Analysis complete for policy (SS14807031), coverage (01).
TEXT
a = ‘02’

p s[/Analysis complete for policy ([^()]*?), coverage (#{a})/]

If the number is an int:

$ ruby x.rb
“Analysis complete for policy (SS14807031), coverage (02)”
$ cat x.rb
s = <<TEXT
Analysis complete for policy (SS14807031).
Analysis complete for policy (SS14807031), coverage (05).
Analysis complete for policy (SS14807031), coverage (02).
Analysis complete for policy (SS14807031), coverage (01).
TEXT
a = 2

p s[/Analysis complete for policy ([^()]?), coverage (0#{a})/]

And if you only need the code inside brackets:

$ ruby x.rb
“SS14807031”
$ cat x.rb
s = <<TEXT
Analysis complete for policy (SS14807031).
Analysis complete for policy (SS14807031), coverage (05).
Analysis complete for policy (SS14807031), coverage (02).
Analysis complete for policy (SS14807031), coverage (01).
TEXT
a = 2

p s[/Analysis complete for policy (([^()]?)), coverage (0#{a})/,
1]

Kind regards

robert

hi Robert K.

Thank you for other alternative ways.

RAJ

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs