jacchr
September 5, 2019, 12:09pm
1
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?
pokular
(Allison Griggs)
September 5, 2019, 12:14pm
2
@jacchr Please try the RegEx Builder.
HareeshMR
(Hareesh Madasi)
September 5, 2019, 12:19pm
3
Can you try this @jacchr
if the string contains the word string every time
jacchr
September 5, 2019, 12:23pm
4
@HareeshMR it does not contain the word “string” every time The text before the last set of parentheses will change every time.
HareeshMR
(Hareesh Madasi)
September 5, 2019, 12:25pm
5
@jacchr
Atleast you will have space before the parenthesis ?
vickydas
(vicky)
September 6, 2019, 5:18am
6
Hello @jacchr
you can use this pattern
(?<=\w\s\()\w+\s\d+|(?<=\w\s\()\w+\s+\w+\s\d+
regex101: build, test, and debug regex
1 Like
jacchr
September 6, 2019, 5:45am
7
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.
jacchr
September 6, 2019, 6:31am
9
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.
VISHNU07
(Mahavishnu.N)
September 6, 2019, 10:16am
12
Hi @jacchr
The below regex code will satisfy all the validation rule
Plz Check it,
Code:
\(([a-zA-Z]{3,}.*?[0-9]{3,})\)
VISHNU07
(Mahavishnu.N)
September 6, 2019, 10:30am
13
second soluton
Regular Expression:
\(([a-zA-Z]{3,}[^\n]*?[0-9]{3,})\)
jacchr
September 6, 2019, 11:44am
14
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
vickydas
(vicky)
September 6, 2019, 11:50am
15
Hello @jacchr
vickydas:
Hello @jacchr
Try this \w+\s\d+[^\()](?=[()]*$)|\w+\s\w+\s\d+[^\()](?=[()]*$)
this will get data inside last parentheses
https://regex101.com/r/ENITXZ/4
Did you check this code ?? it’ll only get the last value
VISHNU07
(Mahavishnu.N)
September 6, 2019, 11:58am
16
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