SchweppesAle Posted April 26, 2010 Share Posted April 26, 2010 Hi, my regex is pretty bad. I was wondering if someone could tell me exactly what the following pattern is removing from the $text string return preg_replace('[\D+]', '', $text); Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/ Share on other sites More sharing options...
Ken2k7 Posted April 26, 2010 Share Posted April 26, 2010 Nothing? There's an error in the regexp. Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048758 Share on other sites More sharing options...
SchweppesAle Posted April 26, 2010 Author Share Posted April 26, 2010 Nothing? There's an error in the regexp. lol, well that explains a lot. How about the following? I found this one in a separate function, it looks somewhat similar though. return preg_replace('[\D+\-]', '', $text); Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048760 Share on other sites More sharing options...
cags Posted April 26, 2010 Share Posted April 26, 2010 Nothing? There's an error in the regexp. No there isn't. It will replace any character that is not a decimal digit. Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048773 Share on other sites More sharing options...
Ken2k7 Posted April 26, 2010 Share Posted April 26, 2010 Hmm.. I always thought you needed delimiters but I looks like the brackets work without them. SchweppesAle, Not sure what the +\- are for. They're redundant. Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048776 Share on other sites More sharing options...
cags Posted April 26, 2010 Share Posted April 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048785 Share on other sites More sharing options...
Ken2k7 Posted April 26, 2010 Share Posted April 26, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048788 Share on other sites More sharing options...
cags Posted April 26, 2010 Share Posted April 26, 2010 You can use them as normal, without escaping them. The engine is clever enough not to confuse them with the delimiters. Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048790 Share on other sites More sharing options...
salathe Posted April 26, 2010 Share Posted April 26, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048793 Share on other sites More sharing options...
Ken2k7 Posted April 26, 2010 Share Posted April 26, 2010 Thanks for the clarification salathe. Thanks to you too cags. I learned something new. Probably not the most useful, but it's something. Quote Link to comment https://forums.phpfreaks.com/topic/199806-basic-regex-patternpreg_replace/#findComment-1048797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.