[python] regular expression

Questions about programming languages and debugging
Post Reply
User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

[python] regular expression

Post by moudy »

hello every one
I was messing around with re module in python, i came upon this and i can't figure it out !
is there a compact expression to find alphanumeric characters including special characters like

Code: Select all

.-
\w is used for alphanumeric alone !
thanks for any help
mahmoud_shihab@hotmail.com

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: [python] regular expression

Post by Lundis »

you can put everything you want in brackets:

Code: Select all

[a-zA-Z0-9]
I'm not sure if you can put dots and dashes there too, but it's worth a try. You might have to backslash them though.

User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

Re: [python] regular expression

Post by moudy »

Yes you're right Lundis, I have to backslash them
I'm discovering the potentials of regular expressions in coding
thanks for the reply.
mahmoud_shihab@hotmail.com

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: [python] regular expression

Post by Gogeta70 »

Regex can get very confusing very fast. One problem beginners tend to have is something called 'greedy regex'. For example, say you want to parse some bb code, let's say a url tag, like so:

[ URL=http://google.com/]Go to google![/URL ]

You may try a regex like this:

#\\].+\[/URL\]#i (the i at the end is to indicate case insensitive regex)

This expression seems like it will work fine, but the problem is that .+ part. That .+ part will chomp all the way through the entire string that you give it until it reaches the last
in the code, thus why it is called 'greedy'. This can be prevented in two ways:

.+? <-- very simple and effective.
[^\[] <-- Searches for everything that isn't a '[' until it reaches one.

Anyway, that's all. Also keep in mind that the regex i provided above is simplified for ease-of-reading and that it is vulnerable to xss attacks. You should always make sure that the urls provided start with http:// or ftp://

Anyway, have fun learning regex! ^_^
¯\_(ツ)_/¯ It works on my machine...

User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

Re: [python] regular expression

Post by moudy »

Thanks gogeta, this was really helpful :D
mahmoud_shihab@hotmail.com

Post Reply