www.webdeveloper.com
Results 1 to 4 of 4

Thread: regular expressions help

  1. #1
    Join Date
    Dec 2011
    Posts
    28

    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
    Location
    Ankh-Morpork
    Posts
    19,616
    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.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2011
    Posts
    28
    Worked fine , Thank you very much

  4. #4
    Join Date
    Apr 2010
    Posts
    88
    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.

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



Recent Articles