tags:

views:

21

answers:

1

Hey guys, I'm having a few issues with using PCRE to make a irc nickname format correctly. I'm not good with PCRE, and I'd love some suggestions from those of you who do use PCRE / regex. :)

I'm currently using this expression: /^([^A-Za-z]{1})([^A-Za-z0-9-.]{0,32})$/ I'm using it as such: preg_replace($regex, $replaceWith, $content)

I assumed this meant, starting from the front to the end, any characters that are not A-Z, a-z, or 0-9 for the first character, replace it. Any characters after that, in which are not A-Z a-z, 0-9, -, or ., replace it.

If anyone could help, you would be helping out greatly. It's the only thing stopping me from releasing a chat product to a new forum software. :/

A: 

I'm not sure what you're trying to replace with, but it'd be better to check if the string matches a username (instead of not matching) and then replace if it doesn't:

$regex = '/^[a-z][a-z0-9.-]{0,32}$/i';
if (!preg_match($regex, $content))
{
  // do your replace here
}

The regular expression says:

^                   # Beginning of string
  [a-z]             # Match a single a-z
  [a-z0-9.-]{0,32}  # Match between 0 and 32 occurances of a-z, 0-9, . or -
$                   # End of string
/i                  # Make the pattern case-insensitive
Daniel Vandersluis
I was trying to replace the character(s) matched with "". Sorry, forgot to mention that. The provided does not seem to work, but I have gotten down to something: "#([^a-z0-9]{0,32})#i" -- it may not do exactly what I need, but I'm using a substr() to continuously remove the first character (if it's a number) until it's a letter, symbol, or blank. If you have any other suggestions, let me know. And thanks for replying/helping :)
Billy Ewing
If you just want to delete invalid characters, use `preg_replace('/[^a-z0-9.-]+/i', '', $content)`. You shouldn't be trying to validate *and* fix the format in the same operation.
Daniel Vandersluis