-1

I have data like the following:

Today's tracker log(F812) was posted at 1500 there will be a new log(F813) tomorrow at 0700.

I am trying to match between each parentheses, instead it is matching almost the entire string.

 \((.*)\)

What am I doing wrong?

2 Answers2

4

* is a greedy quantifier; consuming as much as possible. To make it non-greedy use *?. Once you specify the question mark, you're stating (don't be greedy.. as soon as you find an )... stop, you're done.)

\((.*?)\)
     ^

Live Demo

hwnd
  • 69,796
  • 4
  • 95
  • 132
0

There are 3 things to keep in mind when designing your Regex patterns:

  1. Something must appear
  2. Something must not appear
  3. Something may appear (and how many times)

It it quite typical to match pairs like parenthesis and quotes. Use the following pattern:

\(([^)]+)\)

"(" and ")" are "Must appear's" and between them are "Must not appear's" (means you don't expect to meeet a ")" before it. Watch this demo: http://rubular.com/r/Ki9puMZmBJ

Johnny
  • 481
  • 4
  • 13