RegEx search for a substring within a substring

C

colinhumber

How can I use regular expressions to search for a substring within
another substring? For example, if I have the string:

"This is * some text <button id="123">Some * text</button>"

and I only wanted to find the * character(s) that appear within the
<button> tags, how could I do that using reg ex?

Thanks in advance for the help.
 
O

Oliver Sturm

How can I use regular expressions to search for a substring within
another substring? For example, if I have the string:

"This is * some text <button id="123">Some * text</button>"

and I only wanted to find the * character(s) that appear within the
<button> tags, how could I do that using reg ex?

You'll need to write a regular expression that uses balanced matching,
to make sure that any given * is actually within the scope defined by
the button tag. Have a look at this blog article for information on
balanced matching:

http://blogs.msdn.com/bclteam/archive/2005/03/15/396452.aspx


Oliver Sturm
 
O

Oliver Sturm

Oliver said:
You'll need to write a regular expression that uses balanced matching,
to make sure that any given * is actually within the scope defined by
the button tag.

Or, thinking about it, you don't want to do that at all, because the
button tag can't be nested at all. In that case, try something like this:

<button[^>]*>(.*?(?<asterisk>\*).*?)*</button>



Oliver Sturm
 
S

Sherm Pendley

How can I use regular expressions to search for a substring within
another substring? For example, if I have the string:

"This is * some text <button id="123">Some * text</button>"

and I only wanted to find the * character(s) that appear within the
<button> tags, how could I do that using reg ex?

Thanks in advance for the help.

Are you trying to parse HTML, SGML, XML, or some other markup language
using regular expressions? If so, the Perl FAQ will tell you why that's
a bad idea. Use a parser for parsing.

Also - why the cross-post? Regular expressions are not generic - so an
answer you get from the Perl group won't necessarily apply to a regex
engine that's written in C#, and vice versa. If you have a question about
C# regexes, ask it in the C# group.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top