1

I want to capture all ASCII string within the parentheses before the keyword "end". However, I am only interested in capturing the 1st matching group.

How do I ignore the 2nd matching group?

This is the sample regex which I wrote: \((.+?)\) end

And this is the sample string which I used: "There are some other sentences before (some otherwords which I am not interested in) all these.This is a sample string (something which I am interested in) end. This is another repeated string (with some otherwords) end."

I am only interested to obtain the output "somethings which I am interested in" which is in between the parentheses.

ilovetolearn
  • 2,006
  • 5
  • 33
  • 64

1 Answers1

1

Let me answer to your original question first.

I want to capture all ASCII string within the parentheses before the keyword "end". However, I am only interested in capturing the 1st matching group.

How do I ignore the 2nd matching group?

Input:

There are some other sentences before (some otherwords which I am not interested in) all these.This is a sample string (something which I am interested in) end. This is another repeated string (with some otherwords) end.

Expected capture:

somethings which I am interested in

Regex to use:

^(?<!\) end).*?\(([^()]+?)\) end

Demo: https://regex101.com/r/dVo9Zi/1

Additional notes:

  • In one of your comments you said:

if there are parentheses within the parentheses, we will have issues with the regex. I am not sure if we can even extract such keywords.

If you need to analyze nested structures, you have to forget about regex and for a parser, as explained here: Can regular expressions be used to match nested patterns?

  • If you really mean all ASCII strings in your question, then you will have to adapt [^()] in the regex and replace it by the successive intervals in hexadecimal of all ASCII characters and you will have to explicitly exclude ( and ). This gives you the following character class: [\x00-\x27\x2A-\x7F]. Reference: http://www.asciitable.com/, demo: https://regex101.com/r/dVo9Zi/2
Allan
  • 12,117
  • 3
  • 27
  • 51