RegEx expression - Get text inside parentheses

Hi Experts

I need a RegEx expression that can extract the text inside parantheses but only if a specific text pattern is met. If more than one set of parantheses exists it should only be the text in the last set that should be looked at.

The validation rule should be 1-3 three letters in the beginning and 1-3 numbers at the end of the text inside the parantheses.

Example 1:
(ABC 123) Text string (DEF 456)
Result: DEF 456

Example 2:
(ABC 123) Text string (DEFG 123)
Result: DEFG 123

Example 3:
(ABC 123) Text string (DEFG ABC)
Result:

Example 4:
(ABC 123) Text string (123)
Result:

Example 5:
(ABC 123) Text string (ABC DEF 123)
Result: ABC DEF 123

This is what I have so far:
(?<=()\w{1,3}\s+\d{1,3}?(?=))

But if I use the RegEx on example 1 it will return “ABC 123” from the first set of parantheses. Also it does not support any text between the first three letters or last three digits.

Any idea on how to get around this?

@jacchr Please try the RegEx Builder.

Can you try this @jacchr

if the string contains the word string every time :slight_smile:

image

@HareeshMR it does not contain the word “string” every time :wink: The text before the last set of parentheses will change every time.

@jacchr

Atleast you will have space before the parenthesis :laughing:?

Hello @jacchr
you can use this pattern
(?<=\w\s\()\w+\s\d+|(?<=\w\s\()\w+\s+\w+\s\d+

regex

https://regex101.com/r/ENITXZ/1

1 Like

Hi @vickydas

Great that seems to be working. However I just found another variation of the string where the RegEx won’t work.

Example:
Text string (ABC 123) (DEF 456)

The result will then be ABC 123. How do I tweak the RexEx to always look at the last set of parentheses?

I found this pattern:
(([^)]))[^(]$

It seems to always take the last set of parantheses. However it also includes the parentheses where I only need the text inside.

Hi @vickydas

Thanks - that works but now only if the string has two sets of parentheses. So now it does not work for “My string (DEF 456)”.

I tweaked the other pattern I’ve found:

(?<=()([^)]))[^(]$

However it still includes the last parenthese. Not sure how to leave the ending paranthese out.

Hi @jacchr

The below regex code will satisfy all the validation rule
Plz Check it,

Code:

\(([a-zA-Z]{3,}.*?[0-9]{3,})\)

second soluton

Regular Expression:

 \(([a-zA-Z]{3,}[^\n]*?[0-9]{3,})\)

Hi @VISHNU07

Thanks for your suggestions. What I need is a pattern that only returns what is shown as “result”.

Basically what I need is:

  • Return what is inside the last set of parantheses
  • But only if the string inside the parantheses starts with 1-3 characters and ends with 1-3 digits
  • The parantheses should be left out of the result

So if the string is:
Text string (ABC 123) (DEF 456) additional text

The result returned should be:
DEF 456

Hello @jacchr

Did you check this code ?? it’ll only get the last value

About Regular Expression takes the same data with you are expected
plz use .group function to archive the perfect result

PFA,regex.xaml (8.7 KB)

you will get a idea refer the attachment