Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > RegEx search for a substring within a substring

Reply
Thread Tools

RegEx search for a substring within a substring

 
 
colinhumber@gmail.com
Guest
Posts: n/a
 
      08-03-2005
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.

 
Reply With Quote
 
 
 
 
Oliver Sturm
Guest
Posts: n/a
 
      08-03-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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/archiv...15/396452.aspx


Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
Reply With Quote
 
 
 
 
Oliver Sturm
Guest
Posts: n/a
 
      08-03-2005
Oliver Sturm wrote:

> 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
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      08-03-2005
(E-Mail Removed) writes:

> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with regex and optional substring in search string Timur Tabi Python 4 10-14-2009 10:22 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Getting substring by regex Christine Mayer Java 5 09-07-2007 04:38 AM
regex bug (comments within regex not as robust) kg.google@olympiakos.com Perl Misc 3 10-27-2005 07:21 PM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM



Advertisments