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

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Regex matching characters unless they're at the end of a string


    Not sure how to explain this, but here it goes.

    Code:
    $val
    could contain a string followed by none, one or multiple dots:

    Code:
    test
    test.
    test..
    test... 
    etc
    Using regex I'm looking for any invalid characters in $val and then I want to replace them.

    Code:
    (preg_replace('-['.$inv.']-',"",$val))
    $inv is an array of invalid entries, this also includes a dot.

    How do I strip all $inv characters from $val EXCEPT any dots on the end? eg:

    if $val = te%st... the correct result should be testů

    if $val = te.est. the correct result should be test.

    Hope that makes sense. Thanks.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    what about a "mixed" sequence of invalid characters at the end like in
    Code:
    test%...
    Will the "%" be kept?

    If yes, then simply replace all sequences of invalid characters with a valid character following:
    Code:
    /[INVALID]+(?=[^INVALID])/
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Unfortunately not.

    % is coming from $inv, that needs to be removed as do any dots unless they are at the end
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Code:
    /([INVALID])+(?!\\1*$)/

IMN logo majestic logo threadwatch logo seochat tools logo