Regex matches dynamic pattern

I have a string that I need to parse using Regex (or Match):

SomeTextHereThatIWantToIgnore: <First Month.104.yyyy-mm-dd> <Last Day.2.yyyy-mm-dd>"  OR SomeTextHereThatIWantToIgnore: "<BadVerb.104>"

      

There may be one, two or three lines (all on one line) that I want to parse. In each case, I want to capture everything between "<" and ">".

So, in the first example, I want to capture "First Month.104.yyyy-mm-dd" and "Last Day.2.yyyy-mm-dd". In the second example, I would like to capture "BadVerb.104".

Any Regex gurus who can show me how to do this?

+3


source to share


4 answers


Basic regex to get text inside angle brackets without capturing brackets

(?<=<)[^>]+(?=>)

      



Use Regex.Matches

to get your matches.

var matches = Regex.Matches(str, @"(?<=<)[^>]+(?=>)");

      

+1


source


You can use the following (with a modifier s

for multiple string matches):

(?s)(?<=&lt;).*?(?=&gt;)

      



See DEMO

0


source


Try: &lt;[\s\S]*&gt;

[\ s \ S] captures any space character or any non-space character, so together they will capture anything between &lt;

and &gt;

.

See here

0


source


Thanks, stribizhev, I didn't know about the Matches collection. I ended up making a simple template:

[<]([^>]+)

      

that is, search for "<" and then grab anything that is not ">". The collection of matches is the part I was looking for. I repeated the assembly of Match and was able to parse the resulting string and get what I wanted.

Thanks to Sami, Kartik and Walker (it looks simple too).

0


source







All Articles