• holy regex batman


    last week i was getting real frustrated with some regex madness.  i had a string here:

    >Status Updates</b><br /><form action="home.aspx" method="post">
    <
    input type="hidden" name="post_form_id" value="blah" />Tim is testing....

    and was needing to get the value attribute out.  i was using the regex pattern of:

    \"post_form_id\"\svalue=\"(?<formId>(.*))\"

    and it was giving me the starting point but also the rest of the string at the end.  argh!  i am not an advanced regex guru and was getting pretty frustrated about the solution.  luckily some other people with mad skillz came to my rescue (thanks dino and zain).

    taking the modified pattern to:

    "\"post_form_id\"\\svalue=\"(?<formId>(?:[^\"]*))\""

    gave me the specific outcome i expected and the capture into my group that i needed.  so i was happy.  the explanation of why the first pattern wasn't working was because my ".*" was slurping up all the good stuff including all text until the last double-quote.  to my feeble brain it didn't make too much sense as when i look at the pattern it looked good, but i digress.

    anyhow, in this exercise i learned that zain had a webcast series on regex.  so if you want to be immersed in a series of non-readible characters with all sorts of brackets, head on over to zain's world for his webcast series all about regex.

    tags:

    Monday, August 13, 2007 9:53 AM

    PostTypeIcon

Comments.

  • zain said:
    Gravatar
    # re: holy regex batman


    I think my fav regex ever is the one I saw on http://www.thinkgeek.com/tshirts/coder/57f0/ that had this on it: /(bb|[^b]{2})/

    8/13/2007 11:00 AM
  • Sam Judson said:
    Gravatar
    # re: holy regex batman


    Another alternative is:

    (?<formId>(.*?))

    "*?" is the "non-greedy" version of "*"

    8/13/2007 1:39 PM

Your Reply.

  Comment Form  

Fields denoted with a "*" are required.

*Your name:
Subject:
Your blog:
Your email:  (will not be displayed)
*Your message:

 
Please add 1 and 1 and type the answer here: