471,325 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

REGULAR EXPRESSION

hi.. I am trying to match '+ %&/-' etc using regular expression in
expressions like 879+34343. I tried \W+ but it matches only in the
beginning of the string Plz help Thanking you in advance...

Sep 4 '07 #1
7 1006
AniNair wrote:
hi.. I am trying to match '+ %&/-' etc using regular expression in
expressions like 879+34343. I tried \W+ but it matches only in the
beginning of the string Plz help Thanking you in advance...
Perhaps you could give a few example of strings that should and
shouldn't match? It isn't clear from your description what pattern you
are trying to find.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Sep 4 '07 #2
AniNair wrote:
hi.. I am trying to match '+ %&/-' etc using regular expression in
expressions like 879+34343. I tried \W+ but it matches only in the
beginning of the string Plz help Thanking you in advance...
Is this what you are seeking for?
>>re.compile('(\+{0,1})?([0-9]+)').findall('879+34343')
[('', '879'), ('+', '34343')]

Tuomas Vesterinen
Sep 4 '07 #3
On Sep 4, 6:32 am, AniNair <anitn...@gmail.comwrote:
hi.. I am trying to match '+ %&/-' etc using regular expression in
expressions like 879+34343. I tried \W+ but it matches only in the
beginning of the string Plz help Thanking you in advance...
You may want to read the page describing the regex syntax a little
closer. http://docs.python.org/lib/re-syntax.html

\W would match anything but \w. That is, it would match spaces and
tabs as well as the weird characters. I don't think that's what you
want.

Also, don't forget to use raw strings. r"\W+" is preferred over "\W+".

Sep 4 '07 #4
Steve Holden wrote:
AniNair wrote:
>hi.. I am trying to match '+ %&/-' etc using regular expression in
expressions like 879+34343. I tried \W+ but it matches only in the
beginning of the string Plz help Thanking you in advance...
Perhaps you could give a few example of strings that should and
shouldn't match? It isn't clear from your description what pattern you
are trying to find.

regards
Steve

If it's operations between two numbers try :
r'([\d.]+?)\s*([-+/*%&])([\d.]+)'
It will give you 3 groups, first number, operation and second number
(caveat emptor).
Sep 4 '07 #5
On Sep 5, 4:35 am, Ricardo ArŠoz <ricar...@gmail.comwrote:
Steve Holden wrote:
AniNair wrote:
hi.. I am trying to match '+ %&/-' etc using regular expression in
expressions like 879+34343. I tried \W+ but it matches only in the
beginning of the string Plz help Thanking you in advance...
Perhaps you could give a few example of strings that should and
shouldn't match? It isn't clear from your description what pattern you
are trying to find.
regards
Steve

If it's operations between two numbers try :
r'([\d.]+?)\s*([-+/*%&])([\d.]+)'
It will give you 3 groups, first number, operation and second number
(caveat emptor).

Hi.. Thanks alot for finding time to help a beginner like me. What I
am trying to do is validate the input i get. I just want to take
numbers and numbers only. So if the input is 23+1 or 2/3 or 9-0 or
7/0 , I want to find it using reg exp. I know there are other ways to
do this... but i thought i will try this as i need to learn reg exp. I
tried \D+ , \W+, and \D+|\W+ .. Thanks once again...

Sep 5 '07 #6
AniNair wrote:
On Sep 5, 4:35 am, Ricardo ArŠoz <ricar...@gmail.comwrote:
>Steve Holden wrote:
>>AniNair wrote:
hi.. I am trying to match '+ %&/-' etc using regular expression in
expressions like 879+34343. I tried \W+ but it matches only in the
beginning of the string Plz help Thanking you in advance...
Perhaps you could give a few example of strings that should and
shouldn't match? It isn't clear from your description what pattern you
are trying to find.
regards
Steve
If it's operations between two numbers try :
r'([\d.]+?)\s*([-+/*%&])([\d.]+)'
It will give you 3 groups, first number, operation and second number
(caveat emptor).


Hi.. Thanks alot for finding time to help a beginner like me. What I
am trying to do is validate the input i get. I just want to take
numbers and numbers only. So if the input is 23+1 or 2/3 or 9-0 or
7/0 , I want to find it using reg exp. I know there are other ways to
do this... but i thought i will try this as i need to learn reg exp. I
tried \D+ , \W+, and \D+|\W+ .. Thanks once again...
Well \d will match a number and '.' inside [] will match a dot ;) so if
you want only integer numbers ([\d.]*) should be replaced with (\d+)
Only problem with the expression I sent is that it will match a number
with more than one dot.

HTH
Sep 5 '07 #7
Hi.. Thanks alot for finding time to help a beginner like me. What I
am trying to do is validate the input i get. I just want to take
numbers and numbers only. So if the input is 23+1 or 2/3 or 9-0 or
7/0 , I want to find it using reg exp. I know there are other ways to
do this... but i thought i will try this as i need to learn reg exp. I
tried \D+ , \W+, and \D+|\W+ .. Thanks once again...
--

Send a couple of strings and the output you would like from each.
Sep 5 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Buddy | last post: by
4 posts views Thread by Neri | last post: by
11 posts views Thread by Dimitris Georgakopuolos | last post: by
3 posts views Thread by James D. Marshall | last post: by
7 posts views Thread by Billa | last post: by
9 posts views Thread by Pete Davis | last post: by
25 posts views Thread by Mike | last post: by
1 post views Thread by NvrBst | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.