FishermansFriend
FishermansFriend

Reputation: 52

Match only closing brackets

In the following string I have to find only 1) and 2):

"Dsfgdsf ghdsgtaq sadf 5hs a sdgewrg1) AF AFDS (1,1-3). sdfwurf sgwefasöpopwe qdasda (2,3-29). jkgwgvsd sdfawefas2)"

With \d\) I find all closing brackets.

With \((.*?)\) I find (1,1-3) and (2,3-29).

How do I manage to combine both patterns?

Upvotes: 1

Views: 1575

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

It seems you need to match 1 or more digits with a ) after them only if preceded with a letter.

You may use

(?<=\p{L})\d+\)

See the regex demo.

Details

  • (?<=\p{L}) - a positvie lookbehind requiring a letter to be present immediately to the left of the current position
  • \d+ - 1+ digits
  • \) - a literal ).

Upvotes: 1

Related Questions