Anakin001
Anakin001

Reputation: 1256

Regex - Find text between specific characters containing other specific text

Let's say I have the following string:

{sometext1Asometext2};{sometext3Bsometext4};{sometext5Csometext6}

I want to match this:

{sometext3Bsometext4}

So the text between brackets, but not all matches, only the one containing B.

On the internet I can only find examples of matching everything between 2 characters, not only text containing some specific string inside.

The values of 'sometext' are unknown.

Upvotes: 0

Views: 50

Answers (1)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

Try this Regex:

{[^}B]*B[^}]*}

Click for Demo

OR

{(?=[^}]*B)[^}]*}

Click for Demo

Explanation(1st regex):

  • { - matches {
  • [^}B]* - matches 0+ occurrences of any character that is neither a } nor B
  • B - matches B
  • [^}]* - matches 0+ occurrences of any character that is not a }
  • } - matches }

Upvotes: 1

Related Questions