1

I'm new to regular expressions and try to extract text in a string which starts with a value in brackets on the beginning of a new line until the next string in brackets.

My string:

   (1x) cat
   dog
   (2) ele(4)phant
   tiger
   (x) fish 
   bird

I need to get:

- "1x" and "cat\r\ndog"
- "2" and  "ele(4)phant\r\ntiger"
- "x" and "fish\r\nbird"

My regex:

(\r\n)*(\((.*?)\))(.*)

This gets me:

Match 1
Full match  0-8 `(1x) cat`
Group 2.    0-4 `(1x)`
Group 3.    1-3 `1x`
Group 4.    4-8 ` cat`

Match 2
Full match  13-28   `(2) ele(4)phant`
Group 2.    13-16   `(2)`
Group 3.    14-15   `2`
Group 4.    16-28   ` ele(4)phant`

Match 3
Full match  35-44   `(x) fish `
Group 2.    35-38   `(x)`
Group 3.    36-37   `x`
Group 4.    38-44   ` fish `

The problem is that my regex seems to stop at the end of the line so the strings on the new line (dog, tiger, bird) are missing.

Do you have an idea how to also get the content of the next lines until the next match?

1 Answer 1

1

You may use

'~^\(([^()]*)\)(.*(?:\R(?!\([^()]*\)).*)*)~m'

See the regex demo

Details

  • ^ - start of a line (due to m modifier, ^ matches the start of a line rather than the start of the whole string)

  • \( - a (

  • ([^()]*) - Group 1:
    • [^()]* - 0+ chars other than ( and ) (you might use your .*? here, if you do not want to overflow across lines, and want to match ( inside (...))
  • \) - a ) char
  • (.*(?:\R(?!\([^()]*\)).*)*) - Group 2:
    • .* - the rest of the line
    • (?:\R(?!\([^()]*\)).*)* - 0+ sequences of
      • \R(?!\([^()]*\)) - line break not followed with (...) substring
      • .* - rest of the line
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but this doesn't get me the string between the brackets
@Tobi But you have not been speaking about getting the value between brackets. You asked how to get the text up to the (...) substring on a newline. If you need to get the value inside parentheses without ( and ), just re-adjust the first capturing group bounds. See this regex demo

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.