2

I am porting code from Node.js to PHP and keep getting errors with this regular expression:

^/[a-z0-9]{6}([^0-9a-z]|$)

PHP complains about a dollar sign:

Unknown modifier '$'

In JavaScript I was able to check if a string was ending with [^0-9a-z] or END OF STRING. How do I do this in PHP with preg_match()?

My PHP code looks like this:

<?
    $sExpression = '^/[a-z0-9]{6}([^0-9a-z]|$)';
    if (preg_match('|' . $sExpression .  '|', $sUrl)) { 
        // ... 
    }
?>

The JavaScript code was similar to this:

var sExpression = '^/[a-z0-9]{6}([^0-9a-z]|$)';
var oRegex      = new RegExp(sExpression);
if (oRegex.test(sUrl)) { 
    // ... 
}
10
  • 1
    $ has a special meaning in regex. You have to escape that. Commented Jul 8, 2014 at 5:17
  • I am trying to match "END OF TEXT" with $ Commented Jul 8, 2014 at 5:18
  • Then he will have to put it outside the group. Commented Jul 8, 2014 at 5:18
  • what php function are you using such as preg_match, preg_match_all, and well you do have this error "/[" literal "[" not character group. ^/[a-z0-9]{6}([^0-9a-z]|$) Commented Jul 8, 2014 at 5:18
  • 1
    What was the original JavaScript code? Commented Jul 8, 2014 at 5:18

3 Answers 3

6

To match a string that starts with a slash, followed by six alphanumerics and is then followed by either the end-of-string or something that's not alphanumeric:

preg_match('~^/[a-z0-9]{6}([^0-9a-z]|$)~i', $str);

The original JavaScript probably used new RegExp(<expression>), but PCRE requires a proper enclosure of the expression; those are the ~ characters I've put in the above code. Btw, I've made the expression case insensitive by using the i modifier; feel free to remove it if not desired.

You were using | as the enclosure; as such, you should have escaped the pipe character inside the expression, but by doing so you would have changed the meaning. It's generally best to choose delimiters that do not have a special meaning in an expression; it also helps to choose delimiters that don't occur as such in the expression, e.g., my choice of ~ avoids having to escape any character.

Expressions in PCRE can be generalised as:

<start-delimiter> stuff <end-delimiter> modifiers

Typically, the starting delimiter is the same as the ending delimiter, except for cases such as [expression]i or {expression}i whereby the opening brace is matched with the closing brace :)

Sign up to request clarification or add additional context in comments.

4 Comments

Jack, you are right, I should have shown the complete code – I am using "|" character as delimiter. So, I guess this is not the problem. Please see my edit in the question.
Yes it is the problem because your delimiter | is also part of your expression, this is why it thinks $ is a flag, because it is right after a |.
@LexPodgorny Well, yeah, the expression itself is fine (I think).
You are absolutely right. Changing | to ~ fixed it. Thank you for taking time to explain!
0

Fix the regular expression first:

^/[a-z0-9]{6}([^0-9a-z]|$)

Try this.

As others pointed out, I'm an idiot and saw a / as a \ ... LOL.

Ok, well go at this again,

I’d avoid using the "|" and just do it this way.

if (preg_match('/^\/[a-z0-9]{6}([^0-9a-z]|$)/', $sUrl)) { ... }

9 Comments

really, you think that is a valid regx?
I don't see how ^/ is valid either. Last I checked it was /^.
so a character group implied by {6} without an opening bracket is good syntax? shoot. lol @ me tired, I saw / as \ but still he needs to escape it surly
@zerkms - i'll blame that on dyslexia, but what is his delimiters is now the question.
He updated his question finally showing what was causing all this confusion. It turns out / wasn't meant to be the delimiter.
|
0

Reducing this to just matching a particular character or end of string (PHP),

\D777(\D|$)\

This will match:

xxx777xxx or xxx777 but not xxx7777 or xxx7777xxx

1 Comment

\D will only match a single non-digital character. Those xxx are misleading.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.