REGEXP Extract value not Groups - regex

I am trying to extract a word from the string which contains a specific word from the list.
For example, there is string "RR_SM_Brand_A_Additive_Clean_jun2020", and the list is Brand_A, Brand_B, Brand_C etc.
When I test RegExp, I receive 3 groups as output not 1 value (Brand_A): https://regexr.com/5tcp7
Code: (.*)(Brand_A|Brand_B)(.*)
I am very new to Regex and I am confused how can I extract only one value: Brand_A

If you don't need to capture the surrounding text, leave them out of the regexp. A regular expression will match anywhere in the input.
Brand_A|Brand_B
In this case, the matches will be in group 0.
Some languages automatically anchor the regexp. In that case, you can put .* around this, but don't put them in capture groups.
.*(Brand_A|Brand_B).*

Try this simple one:
(?:Brand_A|Brand_B)

You are almost right, you need to add non-capturing group (?:) for your outside groups:
(?:.*)(Brand_A|Brand_B)(?:.*)
https://regexr.com/5tcpa

Related

RegEx to match all sets of items that have part of specific value

I'm trying to use RegEx to filter all sets of items that have part of a specific value in a capture group that I have defined.
I have to check if the fifth capture group contains at least part of a specific text.
My string:
First Item;Second Item;Third Item;Fourth Item;First Word;Sixth
Item?First Item;Second Item;Third Item;Fourth Item;Second Word;Sixth
Item?First Item;Second Item;Third Item;Fourth Item;Can't Capture This
Set;Sixth Item
RegEx that works for exact word:
(?:^|\?)([^;]+);([^;]+);([^;]+);([^;]+);(Second Word);([^;\?$]+)
The problem is that I need this RegEx to work to capture only part of the word.
Not Working:
(?:^|\?)([^;]+);([^;]+);([^;]+);([^;]+);(.*Word.*);([^;\?$]+) >
Thanks!
Use [^;]* instead of .* because you have semi-colons as field delimiters:
(?:^|\?)([^;]+);([^;]+);([^;]+);([^;]+);([^;]*Word[^;]*);([^;?]+)
See proof. ([^;]*Word[^;]*) will match zero or more characters other than semi-colons, then a Word and zero or more characters other than semi-colons.

Regular expression to match text after another regex matched text

In general, I would like to match a text with a pattern and match the text after that with another pattern. This sounds blurry I assume, so look at this example:
https://regex101.com/r/i35XhG/1
In the example I am matching for "Chassis ID :" where I do not know the number of spaces between "Chassis ID" and ":", therefore I added \s+. The second capturing group matches a specially formatted series of hexadecimal numbers.
Now my goal is to isolate the hexadecimal part in the result but I only get that together with "Chassis ID :". How can I accomplish this ?
This is a general problem for me to match something dynamic in length, but only care and retrieve what comes afterwards.
Thank you in advance.
All you need to do is to wrap your capturing group into parenthesis, like:
(Chassis ID\s+: )()([0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+)
Now you can access 3c:61:04:65:22:80 by \3.
Look here: https://regex101.com/r/14vr2O/1 now you can see Group 3. with your value.
And also you may simplify your regex to this one:
(Chassis ID\s+: )((?:[0-9a-f]+:){5}[0-9a-f]+)
( and ) create a capturing group in a regex Your first group is capturing Chassis ID\s+: and your second group is capturing nothing. Remove the ( and) around Chassis ID\s+: and move the closing ) of the second capturing group to the end of the regex. Now you can access 3c:61:04:65:22:80 by the first capturing group.
Chassis ID\s+: ([0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+)

Capturing first group and separately third + fourth group (REGEX)

I'm trying to create two separate regexes that capture two specific parts of a string:
Example string: 3027-20171110020655-test-5
I need to capture first: 3027 and second: test-5
The second group may not always have a hyphen in it.
I have so far created: [^\-]+
This creates 4 separate capture groups -- I'm not sure how to narrow it down further.
Add the ^ token to mark the beginning of the string:
^[^\-]+
Then, depending on how your input is formatted, you don't need separate regex's, just put whatever you are trying to capture into capturing groups, using parentheses (())
^([^\-]+)-\d+-(.*)
Test it here
You can use this regex using negated character classes and anchors:
^([^-]+)-[^-]*-([^-]+)(?:-([^-]+))?$
Where last group is an optional match.
RegEx Demo

Regex Expression to allow comma only inside a string (within quotes)and not outside it

I am kind of new to regex. I am looking for a regex expression to add it as a constraint not to allow comma outside a string .
My input is like
"1,212121,121212","Extra_data"
Here the regex expression should not check for comma in the first value within quotes "1,212121,121212" but should check after the quotes including ,"Extra_data" . In short expression should allow comma in a string only inside quotes and not outside.
Kindly help me with the expression.
I think this is what you're looking for, essentially a group of numbers or commas surrounded by parentheses then followed by comma and another phrase (not necessarily numbers) in parentheses. Capturing group #1 gives you "1,212121,121212" and capturing group #2 gives you ,"Extra_data"
("[\d,]+")(,"[^"]+")
It would be helpful to see more of how your input might come in. I think that the biggest question that remains is whether that first group always contain only numbers/commas, or are there sometimes other characters such as letters, underscores, etc in that first group? If that first group contains only numbers, as I've assumed, then this should work. If it doesn't, then this will not work.
Edit:
"\s*(,\s*"[^"]+")
try this
".*?(?=,).*?"
it only extract comma in a string only inside quotes
Try the following regex:
"[^"]*"(,)[^"]*"[^"]*"
It will capture the commas you need. But note that PHP has no support for captures of the same groups. i.e. in your case:
If the input is : "1,212121,121212","Extra_data","hel,lo","a,bc"
It will capture commas before "Extra_data" and "a,bc" but will exclude the comma before "hel,lo". For that you'll have to use recursion.
You can try using this regex.
(^,)|("\s*,\s*")|(,$)
If you find any match for this regex, then the string will be invalid.

Use previous result only inside group regex

I am trying to find all words that has multiple letter, but I have one problem. I need to return whole word, not letters.
I have tried to do this way
(?:(\w)(?=\1{1}))
But this expression also returns letters, but I need to get whole matched word.
Is it possible to make such expression.
You can use this regex:
/(\b\w*(\w)(?=\2)\w*\b)/
And whole words will be available in captured group #1
RegEx Demo