regular expressions.

A

Atul.

Hey All,

I have been playing around with REs and could not get the following
code to run.

import re
vowel = r'[aeiou]'
re.findall(vowel, r"vowel")

anything wrong I have done?

Regards,
Atul.
 
P

Peter Otten

Atul. said:
Yes. You didn't paste the traceback into your message.
import re
vowel = r'[aeiou]'
re.findall(vowel, r"vowel")

['o', 'e']

It works as expected here.

Peter

When I key this input in IDLE it works but when I try to run the
module it wont work.

What's the name of your script? What happens when you run it? Does it print
a traceback? If so, what does it say? Please cut and paste, don't
paraphrase.

Peter
 
A

Atul.

Atul. said:
Yes. You didn't paste the traceback into your message.
import re
vowel = r'[aeiou]'
re.findall(vowel, r"vowel")
['o', 'e']
It works as expected here.
Peter
When I key this input in IDLE it works but when I try to run the
module it wont work.

What's the name of your script? What happens when you run it? Does it print
a traceback? If so, what does it say? Please cut and paste, don't
paraphrase.

Peter

This is something get when I run it like below. it does not print any
output.

atul@atul-desktop:~/Work/work/programs$ python fourth.py
atul@atul-desktop:~/Work/work/programs$
 
A

Atul.

Atul. wrote:
Yes. You didn't paste the traceback into your message.
import re
vowel = r'[aeiou]'
re.findall(vowel, r"vowel")
['o', 'e']
It works as expected here.
Peter
When I key this input in IDLE it works but when I try to run the
module it wont work.
What's the name of your script? What happens when you run it? Does it print
a traceback? If so, what does it say? Please cut and paste, don't
paraphrase.

This is something get when I run it like below. it does not print any
output.

atul@atul-desktop:~/Work/work/programs$ python fourth.py
atul@atul-desktop:~/Work/work/programs$

ok I get it thats coz, I dont print it. right? when I print it does
she it.
 
P

Peter Otten

Atul. said:
Atul. wrote:
Yes. You didn't paste the traceback into your message.
import re
vowel = r'[aeiou]'
re.findall(vowel, r"vowel")
['o', 'e']
It works as expected here.

When I key this input in IDLE it works but when I try to run the
module it wont work.
What's the name of your script? What happens when you run it? Does it
print a traceback? If so, what does it say? Please cut and paste, don't
paraphrase.

This is something get when I run it like below. it does not print any
output.

atul@atul-desktop:~/Work/work/programs$ python fourth.py
atul@atul-desktop:~/Work/work/programs$

ok I get it thats coz, I dont print it. right? when I print it does
she it.

Heureka!
 
D

Diez B. Roggisch

Atul. said:
Atul. wrote:
Yes. You didn't paste the traceback into your message.
import re
vowel = r'[aeiou]'
re.findall(vowel, r"vowel")
['o', 'e']
It works as expected here.
Peter
When I key this input in IDLE it works but when I try to run the
module it wont work.
What's the name of your script? What happens when you run it? Does it print
a traceback? If so, what does it say? Please cut and paste, don't
paraphrase.
Peter
This is something get when I run it like below. it does not print any
output.

atul@atul-desktop:~/Work/work/programs$ python fourth.py
atul@atul-desktop:~/Work/work/programs$

ok I get it thats coz, I dont print it. right? when I print it does
she it.

Yes.

Diez
 
A

Atul.

The same file when I use with the following does not work.

import re
vowel =
r'[u"\u093e"u"\u093f"u"\u0940"u"\u0941"u"\u0942"u"\u0943"u"\u0944"u"\u0945"u"\u0946"u"\u0947"u"\u0948"u"\u0949"u"\u094a"u"\u094b"u"\u094c"]'
print re.findall(vowel, u"\u092f\u093e\u0902\u091a\u094d\u092f\u093e",
re.UNICODE)



atul@atul-desktop:~/Work/work/programs$ python fourth.py
[]
atul@atul-desktop:~/Work/work/programs$


is this the way to use Unicode in REs?

Regards,
Atul.
 
P

Peter Otten

Atul. said:
The same file when I use with the following does not work.

import re
vowel =
r'[u"\u093e"u"\u093f"u"\u0940"u"\u0941"u"\u0942"u"\u0943"u"\u0944"u"\u0945"u"\u0946"u"\u0947"u"\u0948"u"\u0949"u"\u094a"u"\u094b"u"\u094c"]'
print re.findall(vowel, u"\u092f\u093e\u0902\u091a\u094d\u092f\u093e",
re.UNICODE)



atul@atul-desktop:~/Work/work/programs$ python fourth.py
[]
atul@atul-desktop:~/Work/work/programs$


is this the way to use Unicode in REs?

No, u"..." is part of the string, not the character. The regex becomes

# untested
vowel = u'[\u093e\u093f\u0940\u0941\u0942\u0943\u0944\u0945\u0946\u0947\u0948\u0949\u094a\u094b\u094c]'

Peter
 
H

Hrvoje Niksic

Atul. said:
the following does not work.

import re
vowel =
r'[u"\u093e"u"\u093f"u"\u0940"u"\u0941"u"\u0942"u"\u0943"u"\u0944"u"\u0945"u"\u0946"u"\u0947"u"\u0948"u"\u0949"u"\u094a"u"\u094b"u"\u094c"]'

Unfortunately you cannot embed arbitrary Python string constants
(u"...") in regular expressions. What does work is something like:
vowel = u'[\u093e\u093f\u0940\u0941\u0942\u0943\u0944\u0945\u0946\u0947\u0948\u0949\u094a\u094b\u094c]'
re.findall(vowel, u"\u092f\u093e\u0902\u091a\u094d\u092f\u093e")
[u'\u093e', u'\u093e']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top