Regex - get first occurrence [duplicate] - regex

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I have the following string:
EN=sdfsdf, CN=User Name, CN=Users, DC=domain, DC=co, DC=il
I need to return the first string that starts with "CN=" and ends with an ",".
In this case I need to return "User Name".
'CN=.*,'
returns
"CN=User Name, CN=Users, DC=domain, DC=co,"
How can I get the just the first occurrence?

You will need the non-greedy option in your Regex:
CN=.*?,
See also How can I write a regex which matches non greedy?

Related

Regex for substring match [duplicate]

This question already has answers here:
Regex for string contains?
(5 answers)
Closed 7 months ago.
I have list of strings
03000_textbox (57447),
03990_textbox (57499),
03000_textnewbox (57447)
I want to use regex to capture 1st and 2nd elements of list.
Anything that contains substring textbox i.e.
03000_textbox (57447)
03990_textbox (57499)
Here is the regex you're looking for :
[0-9]+_textbox \([0-9]+\)
Live sample : https://regex101.com/r/2oiwcF/1
Don't forget to put a global (g) flag so you can get every match and loop into.

How to match optional group, if it is already matched by main group? [duplicate]

This question already has an answer here:
regexp match anything before and after a word, if it exists
(1 answer)
Closed 9 months ago.
I have input strings like:
any[sym)bol_text
any[sym)bol_text (any[sym)bol_text) any[sym)bol_text
any[sym)bol_text (this_text)
any[sym)bol_text2 (this_text)Fzcj
And I have regexp:
(?<text>[^\r\n]+)(?:\(this_text\))?
But I can't handle strings with (this_text) optional group. It matches by first one, but I don't need this exact text in output
^(?<text>.+?)(?:\(this_text\).*)?$
So yes, last group should contains handling any text and ends with $

Regex check return true for " [duplicate]

This question already has answers here:
Regular expression which matches a pattern, or is an empty string
(5 answers)
Closed 2 years ago.
I have /^\w+( \w+)*$/
It tests for one space between words, and no leading or trailing spaces.
However, it fails on an edge case where it is passed just the blank string "
I have tried:
/^\w"+( \w+)*$/
/^\w\"+( \w+)*$/
/^\w|"+( \w+)*$/
but then it fails my other tests.
Any thoughts are appreciated! Thanks!
You can just add an alternation for the blank:
^$|^\w+( \w+)*$
See live demo.
IMHO it's easy to read and understand.

Regexp for string stating with a + and having numbers only [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
I have the following regex for a string which starts by a + and having numbers only:
PatternArticleNumber = $"^(\\+)[0-9]*";
However this allows strings like :
+454545454+4545454
This should not be allowed. Only the 1st character should be a +, others numbers only.
Any idea what may be wrong with my regex?
You can probably workaround this problem by just adding an ending anchor to your regex, i.e. use this:
PatternArticleNumber = $"^(\\+)[0-9]*$";
Demo
The problem with your current pattern is that the ending is open. So, the string +454545454+4545454 might appear to be a match. In fact, that entire string is not a match, but the engine might match the first portion, before the second +, and report a match.

How to capture repeating groups in Regex (for C#) [duplicate]

This question already has answers here:
Get string between two strings in a string
(27 answers)
Closed 6 years ago.
I've to detect and extract from a string a repeating group of characters and list one part of each captured group.
Here is an example of string to parse: "za e eStartGood1Endds qStartGood2Endsds df"
My Regex is: ".*?(?::Start(.+)End.*?)+"
Expecting groups captured expected: Good1, Good2, etc
My Regex capture is wrong: it seems that (?::Start(.+) is considered as group to capture...
May I miss something?
Thanks!
This regex do the job :
/(?<=Start)(.+?)(?=End)/g
Why not use this pattern: \*{2}Start\*{2}(.*?)\*{2}End\*{2}
For this input string: za e e**Start**Good1**End**ds dq**Start**Good2**End**sds df, it captures Good1 and Good2.
You can play with it here: https://regex101.com/r/dG0dX6/2