Rajagopalan
Rajagopalan

Reputation: 6064

Extracting a substring from a string using `Regexp.new`

I have a string like this:

var = "Renewal Quote RQ00041233 (Payment Pending) Policy R38A014294-1"

I have to extract "Payment Pending" from that string using only the information included in another single string.

The following:

var[/\((.*)\)/, 1]

will extract what I want. I can include the string representation of the regex in the string to be given, and construct the regular expression from it using Regexp.new, but I have no way to achieve the information 1 used as the second argument of [].

Without the second argument 1,

regex_string = '\((.*)\)'
var[Regexp.new(regex_string)]

fetches the string "(Payment Pending)"instead of the expected "Payment Pending".

Can someone help me?

Upvotes: 0

Views: 59

Answers (3)

Cary Swoveland
Cary Swoveland

Reputation: 110755

Assuming there are no nested parenthesis in the string, one way to do that without using a regular expression is as follows.

instance_eval "var[(i=var.index('(')+1)..var.index(')',i)-1]"
  #=> "Payment Pending"

See String#index, particularly the reference to the optional second argument, "offset".

Upvotes: 1

Schwern
Schwern

Reputation: 165586

/\((.*)\)/ is just shorthand for Regexp.new('\((.*)\)').

String#[] takes a regex and a capture group as two separate arguments. var[/\((.*)\)/, 1] is var[Regex, 1].

The important thing to realize is 1 is passed to var[], not the regex.

re = Regexp.new('\((.*)\)')
match = var[re, 1]

Note: you might want to require a named capture group rather than a numbered one. It's very easy to accidentally include an extra capture group in a regex.

Upvotes: 1

sawa
sawa

Reputation: 168269

Not sure what you are trying to do, but you can get rid of capturing groups using a different regex:

var[/(?<=\().*(?=\))/]
# => "Payment Pending"

or

var[Regexp.new('(?<=\().*(?=\))')]
# => "Payment Pending"

Upvotes: 3

Related Questions