RegEx search for a substring within a substring

Discussion in 'Perl Misc' started by colinhumber@gmail.com, Aug 3, 2005.

  1. Guest

    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.
    , Aug 3, 2005
    #1
    1. Advertising

  2. Oliver Sturm Guest

    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/archive/2005/03/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
    Oliver Sturm, Aug 3, 2005
    #2
    1. Advertising

  3. Oliver Sturm Guest

    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
    Oliver Sturm, Aug 3, 2005
    #3
  4. 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
    Sherm Pendley, Aug 3, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christine Mayer

    Getting substring by regex

    Christine Mayer, Sep 6, 2007, in forum: Java
    Replies:
    5
    Views:
    426
    Roedy Green
    Sep 7, 2007
  2. Replies:
    3
    Views:
    746
    Reedick, Andrew
    Jul 1, 2008
  3. Timur Tabi
    Replies:
    4
    Views:
    908
    Timur Tabi
    Oct 14, 2009
  4. Abby Lee
    Replies:
    5
    Views:
    393
    Abby Lee
    Aug 2, 2004
  5. Replies:
    3
    Views:
    158
    Paul Lalli
    Oct 27, 2005
Loading...

Share This Page