Thread: RegEx Problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    4
    Rep Power
    0

    Exclamation RegEx Problem


    Hi All,

    I am trying to match this string:

    [quote="BlackICE":434]

    Using:

    '#\[quote(?:="(.*?)")?:434\]((?!\[quote(?:=".*?")?:434\]).)?#ise'

    Using preg_replace in php, and you may have guessed, it doesnt work.

    Which is really fustrating because it works fine using gskinners tool as:

    \[quote(?:="(.*?)")?:434\]((?!\[quote(?:=".*?")?:434\]).)?

    Does anyone know whats going wrong?
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,744
    Rep Power
    9397
    You can't put variables in single-quoted strings.
    PHP Code:
    '#\[quote(?:="(.*?)")?:' $uid '\]((?!\[quote(?:=".*?")?:' $uid '\]).)?#ise' 
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    4
    Rep Power
    0
    No its ok I left that in by accident but it is replaced prior to matching by a str_replace.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,690
    Rep Power
    6351
    Define "doesn't work." Aside from the fact that it matches a character after the closing bracket, it works fine for me.

    Show your actual code, that's always a good place to start when debugging code.

    If your code is more than 20 lines or requires significant setup (like a database or a form post), write out an example with hard-coded values. Like I said, the code works for me (except for your extraneous .)

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    4
    Rep Power
    0
    The code is pretty tough to summarise.

    However I have made some progress myself, it seems that the double quote is causing all the issues. I have changed the regex code to:

    PHP Code:
    '#\[quote(?:=(.*?))?:$uid\]((?!\[quote(?:=.*?)?:$uid\]).)?#ise' 
    Which differs from the previous, which has double quotes as below:

    PHP Code:
    '#\[quote(?:="(.*?)")?:$uid\]((?!\[quote(?:=".*?")?:$uid\]).)?#ise' 
    So without the quotes it is working as ordered, but as I understand it the double quotation isn't a regex special character and should't need escaped.

    I am really stumped to be honest, as on all the regex testing apps ive tried it works fine.

    Essentially like this:

    PHP Code:
    $message preg_replace('#\[quote(?:="(.*?)")?:$uid\]((?!\[quote(?:=".*?")?:$uid\]).)?#ise','whatever''[quote="Username":$uid]asdasds[/quote]'); 
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,690
    Rep Power
    6351
    Ok, you still haven't given us what you actually want to do. What do you want the result to look like?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,690
    Rep Power
    6351
    Update:
    You need a backslash before the dollar sign, even though you're in single quotes. This works:
    PHP Code:
    echo preg_replace('#\[quote(?:="(.*?)")?:\$uid\]((?!\[quote(?:=".*?")?:434\]).)?#is''whatever''there is a message containing a quote: [quote="Username":$uid]asdasds[/quote] That was a quote'); 
    Though again, you don't say what you're looking for. I assume you have some reason for the 'e' flag at the end of your expression.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo