Home > other >  RegEx: How to select everything up to (and including) the first right bracket only?
RegEx: How to select everything up to (and including) the first right bracket only?

Time:09-04

Here is an extract of my data (the full set is over 2000 lines):

1) (6   9)   (14   13)
2) (18 x 11)   (15 x 8)
...
44) (1 - 5)   (12 x 1)
45) (13 x 4) - (8 x 11)
...
578) (17 x 20)   (11   14)
579) (2   12)   (5   19)
...
1598) (17   6) - (0   16)
1599) (6 - 4) x (7 - 4)
1600) (20 - 15) - (6 x 14)

I want to select everything up to the first right bracket (including the bracket itself) on every line, but not select past that.

Expected results: (With bold/yellow highlight represent the selection)

enter image description here

This is regex code I am using: . ?(\)) I think it should work. But when enter image description here

How can I fix it, so it selects up to the first right bracket only on every line (including the bracket itself).

CodePudding user response:

You may any of:

^[^)] \)
^.*?\)
^\d \)

The third option would make sense if you always expect each line to start off with digits, ending in ).

CodePudding user response:

Your regex does not require a match to be at the start of a line. To add that restriction, prefix it with ^ and then add the m flag so ^ matches with any start of a line (and not only the start of the whole input):

/^. ?(\)) /gm

  • Related