Jump to content

preg_replace not working


Nyla

Recommended Posts

Hansford, that's cool! But how come your method works?

 

I believe " \s " means space, right? Well does preg_replace treat "new lines" (e.g. "traversing lines" as I referred to?) as spaces?

 

If so, why doesn't .* or .*? work? Doesn't that mean "everything up to" ???

 

I'm so dumb.

 

I'll learn fast, though, whatever you tell me.

I believe " \s " means space, right? Well does preg_replace treat "new lines" (e.g. "traversing lines" as I referred to?) as spaces?

 

The \s pattern matches a single whitespace character. This includes the space character, the tabular and the various newline characters.

 

 

 

If so, why doesn't .* or .*? work? Doesn't that mean "everything up to" ???

 

No. As I already said, the meaning of the “.” pattern depends on the regex modifiers. By default, “.” matches any character except a newline. If you want it to match a newline as well, you need the “s” modifier.

 

The “*” is a greedy quantifier: It consumes as many repetitions as possible. On the other hand, “*?” is a non-greedy quantifier, which means it consumes as few repetitions as possible.

 

Using “.” is generally poor practice, especially when combined with a greedy quantifier. What happens is this: The pattern first consumes the entire line or even the entire remaining input (depending on the modifiers). Since there's no input left, the regex parser can't find a match for the next patterns. So it goes one step back and tries again (this is called backtracking). If it still can't find a match, it again goes one step back and tries again. And so on.

 

This trial-and-error procedure is obviously extremely inefficient. In the worst case, the parser has to go all the way back to where it started and then starts over. Not good.

 

When you write regexes, be specific. Tell the parser exactly what you want. If you use vague patterns, then the parser has to guess what you want and will take much longer. I also recommend that you read up the basics of regular expressions. There are tons of online resources and books. Just google for it.

Thank you Jacques, I'm like a "trial-and-error" parser hahaha -- I try and try and try.

But your post is very clear and articulate, and I probably won't have to bother you again for a long time. I appreciate your help, and appreciate you helping me solve the problem at hand.

Thank you again, and happy new year!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.