Results 1 to 4 of 4

Thread: regular expressions help

Hybrid View

  1. #1
    Join Date
    Dec 2011

    regular expressions help

    Hi all,

    I created this regular expression

    $search = preg_match_all("~ HYPERLINK\s(\".+\"){1}~",$text,$matches);

    to match something like :
    HYPERLINK "file:///C:\\Documents%20and%20Settings\\omar\\Local%20Settings\\Temp\\resume.htm"

    It works fine , but the problem is that it sometimes matches the following :

    HYPERLINK "file:///C:\\Documents%20and%20Settings\\omar\\Local%20Settings\\Temp\\resume.htm" \l "con_firstname_#con_firstname_" Ahmed  HYPERLINK "file:///C:\\Documents%20and%20Settings\\omar\\Local%20Settings\\Temp\\resume.htm" \l "con_middlename_#con_middlename_"

    All this text is being matched as one match , I want the first part of it until the first double quotation is matched , and the second one to be a second match ..

    I think the problem is at that part : (\".+\") which means any double quotes followed by anything followed by double quotes, I think I must say any double quotes followed by anything (But not double quotes ) followed by double quotes

  2. #2
    Join Date
    Aug 2004
    One way:
    PHP Code:
    $search preg_match_all("~ HYPERLINK\s(\"[^"]+\"){1}~",$text,$matches); 
    Or you could use an "ungreedy" modifier:
    PHP Code:
    $search preg_match_all("~ HYPERLINK\s(\".+\"){1}~u",$text,$matches); 
    Or make just that specific repetition ungreedy:
    PHP Code:
    $search preg_match_all("~ HYPERLINK\s(\".+?\"){1}~",$text,$matches); 
    It's mainly a choice of which you're more comfortable with so that it's easy to figure out later when you need to modify the code. (I'd probably go with the first one, as I think it's the most explicit.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Apr 2010
    Quote Originally Posted by NogDog View Post
    Or you could use an "ungreedy" modifier:
    PHP Code:
    $search preg_match_all("~ HYPERLINK\s(\".+\"){1}~u",$text,$matches); 
    The ungreedy modifier is the upper case U character.

  4. #4
    Join Date
    Dec 2011
    Worked fine , Thank you very much

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center