regular Expression

A forum for general discussion of the Python programming language.

regular Expression

Postby vijaykumbhani » Wed Aug 14, 2013 4:28 am

Code: Select all
pattern = r'(?is)print (.*?)(?is)'

Str = 'print vijay'

Match = re.compile(pattern)
obj = Match.match(Str)
if obj:
    data = re.findall(pattern,Str)
    print obj.group(0)
    print data


here, it do not print 'vijay'
Last edited by micseydel on Wed Aug 14, 2013 4:50 am, edited 1 time in total.
Reason: Added code tags, locked OP.
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India

Re: regular Expression

Postby casevh » Wed Aug 14, 2013 5:17 am

The special characters *? in (.*?) indicate a minimal match. Since matching 0 characters is valid, the empty string is returned. I think you want:

Code: Select all
>>> re.match('(?is)print (.*)(?is)', 'print vijay').groups()
('vijay',)


casevh
casevh
 
Posts: 56
Joined: Sat Feb 09, 2013 7:35 am

Re: regular Expression

Postby vijaykumbhani » Wed Aug 14, 2013 5:34 am

Thanks buddy
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest