Jump to content

Basic REGEX pattern(preg_replace)


SchweppesAle

Recommended Posts

You do require delimiters, but you can use most of the 'bracket' characters as delimiters. So in the OPs pattern the [ and ] characters are the delimiters, not a character class. The pattern is essentially...

 

'#\D+#'

 

The other pattern...

 

'#\D+\-#'

 

...essentially will only replace a string that consists of 1 or more non decimal digits followed by a dash (-), I believe the escaping of the dash is redundant. Of course this all assumes that the OP is correctly using the patterns and that they aren't supposed to be delimited, thus causing the current characters to have different meanings.

Link to comment
Share on other sites

Hmm.. I always thought you needed delimiters but I looks like the brackets work without them.

 

Bracket pairs can be used as delimiters, but are best avoided since they can cause confusion (as evidenced in this thread); acceptable pairs are (), [], {} and <>.  As for what \D means, that has already been mentioned but a good place to look for that and others is on the backslash page in the PCRE regex section of the PHP manual.

 

So with the [ and ] as delimiters, how would you use them as a character set? Do you escape them? But then how would you match a [ or ] character?

 

You could do [[\D-]] but see the note in my first paragraph about confusion! To match literal square brackets, escape them with a backslash character like \[ or \] (see the same PHP manual page I linked to above).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.