12

I am masking all characters between single quotes (inclusively) within a string using preg_replace_callback(). But I would like to only use preg_replace() if possible, but haven't been able to figure it out. Any help would be appreciated.

This is what I have using preg_replace_callback() which produces the correct output:

function maskCallback( $matches ) {
    return str_repeat( '-', strlen( $matches[0] ) );
}
function maskString( $str ) {
    return preg_replace_callback( "('.*?')", 'maskCallback', $str );
}

$str = "TEST 'replace''me' ok 'me too'";
echo $str,"\n";
echo $maskString( $str ),"\n";

Output is:

TEST 'replace''me' ok 'me too'
TEST ------------- ok --------

I have tried using:

preg_replace( "/('.*?')/", '-', $str );

but the dashes get consumed, e.g.:

TEST -- ok -

Everything else I have tried doesn't work either. (I'm obviously not a regex expert.) Is this possible to do? If so, how?

6
  • You can't get the strlen of the match without the callback. You'd need to match each character individually. Needless to say, the callback is much better here. Why don't you want to use a callback?
    – crush
    Jan 29, 2014 at 18:58
  • Just trying to simplify if possible. Thanks
    – Alan
    Jan 29, 2014 at 19:18
  • 1
    I feel like this is possible using regex and the accepted solution is wrong... but I will have to circle back when I have more time. Jan 29, 2014 at 20:13
  • 2
    In case it simplifies things, I realized that replacing the single quotes is optional for my particular problem. A solution that replaces characters between all pairs of single quotes excluding the single quotes would work too. It was just easier to replace the single quotes when using preg_replace_callback().
    – Alan
    Jan 30, 2014 at 6:40
  • 1
    Looking at 2 regex solutions below, I really hope you keep your callback solution. It is MUCH MORE readable.
    – nhahtdh
    Jan 30, 2014 at 16:23

3 Answers 3

16

Yes you can do it, (assuming that quotes are balanced) example:

$str = "TEST 'replace''me' ok 'me too'";
$pattern = "~[^'](?=[^']*(?:'[^']*'[^']*)*+'[^']*\z)|'~";    
$result = preg_replace($pattern, '-', $str);

The idea is: you can replace a character if it is a quote or if it is followed by an odd number of quotes.

Without quotes:

$pattern = "~(?:(?!\A)\G|(?:(?!\G)|\A)'\K)[^']~";
$result = preg_replace($pattern, '-', $str);

The pattern will match a character only when it is contiguous to a precedent match (In other words, when it is immediately after the last match) or when it is preceded by a quote that is not contiguous to the precedent match.

\G is the position after the last match (at the beginning it is the start of the string)

pattern details:

~             # pattern delimiter

(?: # non capturing group: describe the two possibilities
    # before the target character

    (?!\A)\G  # at the position in the string after the last match
              # the negative lookbehind ensure that this is not the start
              # of the string

  |           # OR

    (?:       # (to ensure that the quote is a not a closing quote)
        (?!\G)   # not contiguous to a precedent match
      |          # OR
        \A       # at the start of the string
    )
    '         # the opening quote

    \K        # remove all precedent characters from the match result
              # (only one quote here)
)

[^']          # a character that is not a quote

~

Note that since the closing quote is not matched by the pattern, the following characters that are not quotes can't be matched because there is no precedent match.

EDIT:

The (*SKIP)(*FAIL) way:

Instead of testing if a single quote is not a closing quote with (?:(?!\G)|\A)' like in the precedent pattern, you can break the match contiguity on closing quotes using the backtracking control verbs (*SKIP) and (*FAIL) (That can be shorten to (*F)).

$pattern = "~(?:(?!\A)\G|')(?:'(*SKIP)(*F)|\K[^'])~";
$result = preg_replace($pattern, '-', $str);

Since the pattern fails on each closing quotes, the following characters will not be matched until the next opening quote.

The pattern may be more efficient written like this:

$pattern = "~(?:\G(?!\A)(?:'(*SKIP)(*F))?|'\K)[^']~";

(You can also use (*PRUNE) in place of (*SKIP).)

3
  • Thank you. This does solve the problem. I realized later that replacing the single quotes is not really needed but was easier to do so with preg_replace_callback(). Could your solution be simplified if I did not need to replace the surrounding single quotes? Thanks
    – Alan
    Jan 30, 2014 at 6:42
  • 1
    @Alan: I added a version without quotes, the pattern is indeed shorter and more efficient than the first. (less tests) Jan 30, 2014 at 7:32
  • Thanks for everyone's input! Going with this (shortest regex) solution.
    – Alan
    Jan 30, 2014 at 15:20
12

Short answer : It's possible !!!

Use the following pattern

'                                     # Match a single quote
(?=                                   # Positive lookahead, this basically makes sure there is an odd number of single quotes ahead in this line
   (?:(?:[^'\r\n]*'){2})*   # Match anything except single quote or newlines zero or more times followed by a single quote, repeat this twice and repeat this whole process zero or more times (basically a pair of single quotes)
   (?:[^'\r\n]*'[^'\r\n]*(?:\r?\n|$)) # You guessed, this is to match a single quote until the end of line
)
|                                     # or
\G(?<!^)                              # Preceding contiguous match (not beginning of line)
[^']                                  # Match anything that's not a single quote
(?=                                   # Same as above
   (?:(?:[^'\r\n]*'){2})*             # Same as above
   (?:[^'\r\n]*'[^'\r\n]*(?:\r?\n|$)) # Same as above
)
|
\G(?<!^)                              # Preceding contiguous match (not beginning of line)
'                                     # Match a single quote

Make sure to use the m modifier.

Online demo.

Long answer : It's a pain :)

Unless not only you but your whole team loves regex, you might think of using this regex but remember that this is insane and quite difficult to grasp for beginners. Also readability goes (almost) always first.

I'll break the idea of how I did write such a regex:

1) We first need to know what we actually want to replace, we want to replace every character (including the single quotes) that's between two single quotes with a hyphen.
2) If we're going to use preg_replace() that means our pattern needs to match one single character each time.
3) So the first step would be obvious : '.
4) We'll use \G which means match beginning of string or the contiguous character that we matched earlier. Take this simple example ~a|\Gb~. This will match a or b if it's at the beginning or b if the previous match was a. See this demo.
5) We don't want anything to do with beginning of string So we'll use \G(?<!^).
6) Now we need to match anything that's not a single quote ~'|\G(?<!^)[^']~.
7) Now begins the real pain, how do we know that the above pattern wouldn't go match c in 'ab'c ? Well it will, we need to count the single quotes...

Let's recap:

a 'bcd' efg 'hij'
  ^ It will match this first
   ^^^ Then it will match these individually with \G(?<!^)[^']
      ^ It will match since we're matching single quotes without checking anything
        ^^^^^ And it will continue to match ...

What we want could be done in those 3 rules:

a 'bcd' efg 'hij'
1 ^ Match a single quote only if there is an odd number of single quotes ahead
2  ^^^ Match individually those characters only if there is an odd number of single quotes ahead
3     ^ Match a single quote only if there was a match before this character

8) Checking if there is an odd number of single quotes could be done if we knew how to match an even number :

(?:              # non-capturing group
   (?:           # non-capturing group
      [^'\r\n]*  # Match anything that's not a single quote or newline, zero or more times
      '          # Match a single quote
   ){2}          # Repeat 2 times (We'll be matching 2 single quotes)
)*               # Repeat all this zero or more times. So we match 0, 2, 4, 6 ... single quotes

9) An odd number would be easy now, we just need to add :

(?:
   [^'\r\n]*     # Match anything that's not a single quote or newline, zero or more times
   '             # Match a single quote
   [^'\r\n]*     # Match anything that's not a single quote or newline, zero or more times
   (?:\r?\n|$)   # End of line
)

10) Merging above in a single lookahead:

(?=
   (?:              # non-capturing group
      (?:           # non-capturing group
         [^'\r\n]*  # Match anything that's not a single quote or newline, zero or more times
         '          # Match a single quote
      ){2}          # Repeat 2 times (We'll be matching 2 single quotes)
   )*               # Repeat all this zero or more times. So we match 0, 2, 4, 6 ... single quotes
   (?:
      [^'\r\n]*     # Match anything that's not a single quote or newline, zero or more times
      '             # Match a single quote
      [^'\r\n]*     # Match anything that's not a single quote or newline, zero or more times
      (?:\r?\n|$)   # End of line
   )
)

11) Now we need to merge all 3 rules we defined earlier:

~                   # A modifier
#################################### Rule 1 ####################################
'                   # A single quote
(?=                 # Lookahead to make sure there is an odd number of single quotes ahead
   (?:              # non-capturing group
      (?:           # non-capturing group
         [^'\r\n]*  # Match anything that's not a single quote or newline, zero or more times
         '          # Match a single quote
      ){2}          # Repeat 2 times (We'll be matching 2 single quotes)
   )*               # Repeat all this zero or more times. So we match 0, 2, 4, 6 ... single quotes
   (?:
      [^'\r\n]*     # Match anything that's not a single quote or newline, zero or more times
      '             # Match a single quote
      [^'\r\n]*     # Match anything that's not a single quote or newline, zero or more times
      (?:\r?\n|$)   # End of line
   )
)

|                   # Or

#################################### Rule 2 ####################################
\G(?<!^)            # Preceding contiguous match (not beginning of line)
[^']                # Match anything that's not a single quote
(?=                 # Lookahead to make sure there is an odd number of single quotes ahead
   (?:              # non-capturing group
      (?:           # non-capturing group
         [^'\r\n]*  # Match anything that's not a single quote or newline, zero or more times
         '          # Match a single quote
      ){2}          # Repeat 2 times (We'll be matching 2 single quotes)
   )*               # Repeat all this zero or more times. So we match 0, 2, 4, 6 ... single quotes
   (?:
      [^'\r\n]*     # Match anything that's not a single quote or newline, zero or more times
      '             # Match a single quote
      [^'\r\n]*     # Match anything that's not a single quote or newline, zero or more times
      (?:\r?\n|$)   # End of line
   )
)

|                   # Or

#################################### Rule 3 ####################################
\G(?<!^)            # Preceding contiguous match (not beginning of line)
'                   # Match a single quote
~x

Online regex demo. Online PHP demo

2
  • 1
    Wow, crazy. In my particular problem, I would not have an odd number of single quotes so this is a bit more complex than it needs to be for my problem, but do I appreciate the thorough solution. And maybe it will help others who may have a similar problem with an odd number of single quotes. Thanks!
    – Alan
    Jan 30, 2014 at 6:37
  • @Alan Well I said "odd" since the first one that we matched will make it "even". A bit confusing yeah ...
    – HamZa
    Jan 30, 2014 at 10:06
6
+100

Well, just for the fun of it and I seriously wouldn't recommend something like that because I try to avoid lookarounds when they are not necessary, here's one regex that uses the concept of 'back to the future':

(?<=^|\s)'(?!\s)|(?!^)(?<!'(?=\s))\G.

regex101 demo

Okay, it's broken down into two parts:

1. Matching the beginning single quote

(?<=^|\s)'(?!\s)

The rules that I believe should be established here are:

  1. There should be either ^ or \s before the beginning quote (hence (?<=^|\s)).
  2. There is no \s after the beginning quote (hence (?!\s)).

2. Matching the things inside the quote, and the ending quote

(?!^)\G(?<!'(?=\s)).

The rules that I believe should be established here are:

  1. The character can be any character (hence .)
  2. The match is 1 character long and following the immediate previous match (hence (?!^)\G).
  3. There should be no single quote, that is itself followed by a space, before it (hence
    (?<!'(?=\s)) and this is the 'back to the future' part). This effectively will not match a \s that is preceded by a ' and will mark the end of the characters wrapped between single quotes. In other words, the closing quote will be identified as a single quote followed by \s.

If you prefer pictures...

img

6
  • 1
    Where is the Dolorean? May 14, 2014 at 19:26
  • @CasimiretHippolyte Uh, I don't get the reference :s
    – Jerry
    May 14, 2014 at 19:39
  • After, some search, it is a DeLorean (not a Dolorean). May 14, 2014 at 20:27
  • @CasimiretHippolyte It's in the lookbehind. That technique was referred to as 'back to the future' by an article mentioned by zx81.
    – Jerry
    May 15, 2014 at 3:27
  • 1
    @Kobi Indeed, there had to be a way to decide what was a close quote and what wasn't and that was the only way I could think about. Using backrefs wouldn't work either since captures don't get 'carried over' from the previous match (backrefs would be an option if it was a continuous match). Also, assuming that 'Sally's dog' should be replaced, the above regex would correctly do so. So, it's kind of a grey area IMO that definitely need more specifications.
    – Jerry
    May 15, 2014 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.