0

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?

5
  • 1
    Not sure what you are trying to do. Will var[Regexp.new('\((.*)\)'), 1] not work? Commented Mar 6, 2019 at 7:16
  • 1
    I'd ask why you're having to pass regexes around as strings. Commented Mar 6, 2019 at 7:17
  • @sawa It will work, but I am asking Don't I have the way to convert the entire /\((.*)\)/, 1 into a regular expression? Commented Mar 6, 2019 at 7:19
  • @Schwern Because I have to read the regular expression from the sheet and use it in my program. Commented Mar 6, 2019 at 7:19
  • @sawa I have to write the regular expression into a single cell in Excel which I need to read and use it in my program. Commented Mar 6, 2019 at 7:20

3 Answers 3

3

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"
Sign up to request clarification or add additional context in comments.

Comments

1

/\((.*)\)/ 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.

7 Comments

You might want to add the conclusion of you argument. Perhaps: "so, it is impossible to do that" or "so, it does not make sense to try to do that."
yes, I am aware of that but I have to read the regular expression as a single string from the excel sheet and use it in my program, My program receives the argument inside Regex["]
@Schwern It is impossible/does not make sense to include the 1 information in the regex. Wasn't that your point?
@sawa I'd assumed it was a requirement that the list of regexes has to include a capture group.
@Rajagopalan Can you require that the regexes include a capture group?
|
1

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".

3 Comments

Your answer is educational, but you seem to be missing the point. The issue is to express the entire instruction in a single string.
@sawa, yes, I know.
Thank you. Another way, yes. But it has to be one single expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.