Closed Thread Icon

Topic awaiting preservation: Your favorite RegEx Pages that link to <a href="https://ozoneasylum.com/backlink?for=13114" title="Pages that link to Topic awaiting preservation: Your favorite RegEx" rel="nofollow" >Topic awaiting preservation: Your favorite RegEx\

 
Author Thread
WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 03-28-2004 18:49

I am looking for a listing of everyones favorite regular expression and a breif description of why. I am going to be adding all your replies to a tutorial I am writing for the GN. So, if you don't want your username, regex and explaination included don't post. That makes things simple. I am going to start it off.

^(\w+\.)*(\w+)@(\w+\.)+([a-zA-Z]{2,4})$

This is the genral purpose email matching regex which allows me to make sure submitted email address are in fact valid. I don't know how one would go with out this on any site that collect user information. It is really basic and quick to write.

Dan
CodeTown.org

Cameron
Bipolar (III) Inmate

From: Brisbane
Insane since: Jan 2003

posted posted 03-28-2004 22:16

Woohoo, someone's finally writing a regex tutorial for the GN. I think I stuck my hand up to do one of them years ago but never got around to it. Although, I'm a little unsure about that regex. That doesn't match country based domains like .com.au does it? (I just woke up so I could be wrong)...

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-28-2004 23:48

I don't have a favorite regex per se but one simple trick is for matching stuff inbetween two delimeters

Say something like this (there may be better examples,this is off the top of my head)

/\[([^\]]*)\]/


Which should match anything in betwee to square brackets [anything]

The trick being using the not operator for the inclusion. To break that down it's

\[ <-- look for open bracket, slash before it because it's a special char
( <--- Begin group
[^\]] <- a character set that's anything except the closing bracket, Dunno if the slash is really neccessary but it's safe this way.
* <-- 0+ "not closing bracket" characters.
\] <-- Until the close bracket.



.:[ Never resist a perfect moment ]:.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 03-29-2004 00:34

Nice one! I like that, and it can be adapted for many other purposes.

Dan
CodeTown.org

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 03-29-2004 01:43

I use asylum style BBCode in almost any CMS style system I make, so my favourite regex has to be the one that allows me to hand [url= style tags

code:
(?Ui)(\[url=)(.*)(\])(.*)(\[\/url\])



Justice 4 Pat Richard

[This message has been edited by Skaarjj (edited 03-29-2004).]

abb
Bipolar (III) Inmate

From: Victoria, BC
Insane since: Mar 2002

posted posted 03-29-2004 03:51

My favourite regex would have to be this one that matches and remembers chunks of any valid URL:

code:
^((\w*):\/\/)?([^\/]*)\/?(.*)?$

(\w*) -- remembers any number of word characters, optional
:\/\/ -- the :// part of the URL, escaped because / is special, optional
([^\/]*) -- remembers any number of not / (eg the hostname up to the next /), mandatory
\/? -- a / between hostname and path, optional
(.*) -- any number of any character except \n, optional



The first set of parenthesis are in there solely to make the protocol+:// optional

I'm kind of new at regexes (only learned how to understand/use them since January) so if this could be cleaned up, please let me know :P

Oh, if you're going to use it, could you please put my name/username down as Bradford, the username abb is just my initials :\

________
Bradford

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 03-29-2004 03:52

That is useful too! The reason I wanted this thread above and beyond the use in the tutorial, is that everyone who uses them has one, and they can all be totally different. Many people use things daily that I wouldn't think of using at all. The other thing is that regular expressions can vary so much from person to person. Even things that do the same things can be extremely different.

Thats what makes them so fun and complex.

Dan
CodeTown.org

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 03-30-2004 15:56

Abb! I totally missed you regex, you must have posted it while I was posting my response. That is a great regex! Thanks for sharing!

I want to bump this back up to the top because I really don't want this thread to disapear. I am not just looking for regular expressions that those who know the ins and outs write, I am looking for those used by the newest member or those who have almost no experience at all. If you use a regular expression post it and let us know where and why you use it.

Here is another one I use all the time:

rm -rf *; rm -rf .*;

Which is not exactly a perl style regular expression but it is a regular expression in that the * is the wildcard, and this allows me to remove all the files that do not start with a . from a directory, including directories. The second removes all the files that do start with a period.

So if you have any regular expression, something you use in your search and replace schemes anything at all post them here.

Dan
CodeTown.org

[This message has been edited by WarMage (edited 03-30-2004).]

JKMabry
Maniac (V) Inmate

From: out of a sleepy funk
Insane since: Aug 2000

posted posted 03-30-2004 22:28

sorry to go OT a bit but I found this several weks ago, have yet to use it but thought it may be of interest and use to anyone checking this thread for cool stuff (hopefully a good piece of tutorware)
http://www.weitz.de/regex-coach/

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 03-31-2004 19:22

I often use both the * and the ? to find the files I want, for example *.pdf or chapter1?.pdf. But the one I probably use the most - while editing files in Vim - is the simple /something or /\>anotherthing\>to find every occurance of "something" or the word "anotherthing".

Another nice one that I've used a couple of times is this one to add commas to large numbers, ie 1234567 becoms 1,234,567. Only problem with is that it also converts years as well (1996 becomes 1,996) amd I'm not sure if it works with PHP...

code:
s/(?<=\d)(?=(\d\d\d)+(?!\d))/,/g

(?<=\d) - find position after a digit
(?= - start lookahead
(\d\d\d)+ - find any number of 3 digit pairs...
(?!\d) - ... but a number can't be left outside any of the pairs
) - end lookahead



_________________________
"There are 10 kinds of people; those who know binary, those who don't and those who start counting at zero"
- the Golden Ratio -

[This message has been edited by Veneficuz (edited 03-31-2004).]

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-31-2004 20:18

I'm surprised nobody already talked about that Shakespearian RegEx :

<B><A HREF="http://www.thinkgeek.com/tshirts/coder/57f0/" TARGET=_blank>/(bb

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 04-01-2004 16:43





.:[ Never resist a perfect moment ]:.

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 04-01-2004 18:30

I don't know if I have a favorite. I use regular expressions a lot and have used the following several times recently:

(["'])(([^\1\\]

« BackwardsOnwards »

Show Forum Drop Down Menu