Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping out angle brackets with a regular expression

Tags:

regex

Having the following string:

<str1> <str2> <str3>

I am trying to do the regex such that I get the following 3 strings in C:

str1
str2
str3

I am trying to use the following regex but it doesn't seem to be yielding what I am aiming for:

<[a-zA-Z0-9]*.>

According to http://www.myregextester.com/index.php, that regex is going to yield

<str1>
<str2>
<str3>

How can I take out the <>'s?

Also, I'd also like to ONLY match strings with format, i.e., with 3 <>'s, no more, no less. How to approach that?

like image 994
devoured elysium Avatar asked May 20 '26 01:05

devoured elysium


2 Answers

This can be easily done with a perl-compatible regular expression like this:

<([^>]+)>

You just have to make sure to tell your library to search for all matches, instead of trying to match the regular expression against the whole string. You will end up with str1, str2 and str3 as the first group match then.

like image 123
Jim Brissom Avatar answered May 22 '26 04:05

Jim Brissom


What if you change it for <([a-zA-Z0-9]*.)>

like image 41
Arnaud Avatar answered May 22 '26 05:05

Arnaud