Forum Moderators: coopster
My problem:
to remove any single quotes, double quotes or dollar signs from many strings.
I wandered around to al of the tutorials that we have posted here many times and read through. I already understood what they were but didnt really understand them very well. I found a very good tutorial that I don't know if we mentioned
www.english.uga.edu/humcomp/perl/regex2a.html
I also went around to a bunch of other sites as well. I quickly realized that my problem was too simple to find an explanation for. You can find explanations for every complex match under the sun but strip quotes, no sir (or madame).
I came up with this after having a ton of errors.
$var = preg_replace('/[\"\'($)]/',"",$row[1])
now this works but I am sure that I made errors or during my trial and error left things in that I don't need.
Just looking for a little clarification. No homework, I can do that myself. ;)
From what I gathered [] makes a character class so I have all three things I want to replace with nothing in there.
I need to escape the single and double quote so it doesn't blow up.
I need to put parentheses around the $ because it is a metacharacter and otherwise it blows up.
It also seemed to complain with out the / / at the start and end, I understand those to be pattern delimiters (since that is the error I was getting).
So, did I get a clue or still clueless?
your solution seems to work, but usually you define character classes like this:
$var = preg_replace('/[\"¦\'¦\$]/',"",$row);
like [char1¦char2¦char3]
The backslash before the special characters tells the regex engine that this has to be treated as a normal character and not as a special character.
All the best,
Hannes.
My little pattern seems to be a bit slow, not that I really care as long as it does it's job, are there any better methods of implementing this or are preg_replace's always a little slow?
jatar- those pattern delimiters can be anything, i got into the habit of using single quotes, and just escape any of these in your regex "'^$¦()[]?+*{}.
afaik a good way to speed them up is to anchor them, by having the regex search anchored to the beginning or end of your variable....though I'm guessing you won't be able to do that if its a long string with matches throughout.
if youre taking a few hours looking for more info i highly recommend the book "mastering regular expressions" by Jeffrey Friedl. Though its not centered on PHP regex, its pretty good reading about regex using any language with good examples... maybe not bedtime reading though as they can get a bit confusing ;)
To be honest data integrity is much more important than speed so it has to be very thorough, not lightning quick.
That book has been at the top of my to buy list for a while BOL, nothing like a little light reading.
[edited by: jatar_k at 9:39 pm (utc) on July 22, 2003]
type regular expressions into google
yep, read through most of the top 10 already and I do have a good chapter in my Perl in a nutshell book. I also read through all of the links that people have posted.
I still don't really need them often and I have a good book understanding but writing the first one is a little confusing. I also know that a lot of people have a lot of trouble with them.
It may help people understand better that there are no stupid questions and even though I am experienced with PHP doesn't mean I won't ask a noob question on regex. ;) Sometimes I think people worry too much about asking, what they think, are basic or stupid questions.
It is just time that I became more familiar with them, I have been meaning to for a while. I have just never made the time.