Forum Moderators: coopster

Message Too Old, No Replies

Understanding my first regex

at least that I wrote myself

         

jatar_k

6:58 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I know we've been over this a million times but it is funally time. I get around them all the time but in this particular instance I would have had many str_replace so I figured it was time.

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?

captainhannes

8:21 pm on Jul 22, 2003 (gmt 0)

10+ Year Member



jatar_k,

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.

jatar_k

8:50 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah, the pipe between the individual characters. (also for anyonw who doesn;t know, you have to replace ¦ with a real pipe, WebmasterWorld breaks them ;))

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?

vincevincevince

9:03 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



preg_replace IS always a bit slow
str_replace is faster if you can use it
i advise you to do a test - comparing the speed of doing that as 3 str_replace vs. 1 preg_replace
i believe that ereg_replace is faster than preg_replace as well

brotherhood of LAN

9:11 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not using ¦'s should work all the same, you can have more than one character in the class and each character in the preg_replace is evaluated against all of them.

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 ;)

jatar_k

9:26 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The anchoring should be possible, the quotes should only be at either the beginning or end of the string and the strings shouldn't be too long. When I say slow we aren't talking much, 100's of a sec, but it msut be scalable, so I will be measuring very carefully. I have some add ons to work into the parser as well so it will be a work in progress for a month or so.

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]

vincevincevince

9:30 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for learning regex - you are doing the very best course there is... the course of learn by doing, bit by bit :-) solving each problem as you need to gets quickly faster as you remember more and more. and type regular expressions into google - the top two pages are both great resources and cover most you need to know :-D

jatar_k

10:04 pm on Jul 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



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.

sniser

1:45 am on Jul 23, 2003 (gmt 0)

10+ Year Member



There is a great freeware app called The Regex Coach for Linux and Windows - I really recommend looking for that. It's a must have swiss army knife kind of thing =D