Results 1 to 3 of 3

Thread: PHP Regex to find text in quotes with a line break

  1. #1
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Regex to find text in quotes with a line break

    Hi All,

    I have this regex pattern to find text in quotation marks - '/named([\s]|[\r\n])"([^"]+)"/i'


    the strings I am looking for always have 'named ' before the "name" part. This works for most of the file I search through. However, sometimes the file I get has a line break between named and "name" and sometimes it has a line break and a tab between them.
    i.e
    named "name 1" -- Works

    named
    "name 2" -- Doesn't work

    named
    [tab]"name 3" -- Doesn't work

    Can anyone tell me what I am doing wrong?

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    well, with '/named([\s]|[\r\n])"([^"]+)"/i' you are looking for "named " followed by either a whitespace or a \r\n which is also a type of whitespace, but not both together, such as \s\r\n. In this case, the best solution is to not worry about the different types or combinations of whitespace that you may use and just use '/named\s*?"([^"]+)"/i'. The \s will match any type of whitespace whether it be tab, carriage return, newline, or space. *? in this case says that the immediately preceding character can be repeated as little as 0 times or as many as it takes until it reaches the first available double quote.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Fantastic!
    Thank you so much james438 I've spent the better of 3 days trying to get my head around this

Bookmarks

Posting Permissions

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