Fun with regular expression

6 views
Skip to first unread message

linux tutor

unread,
Aug 2, 2015, 8:15:51 AM8/2/15
to Lamp_tutorial
All,

The regular expression is acting bit strange. Need your suggestions ?

Use case 1: ( This is working correct )

This is working as expected.

In [97]: test='aaaaaa'

In [100]: reg7 = re.compile('a{3,5}?')

In [101]: reg7.match(test)
Out[101]: <_sre.SRE_Match at 0x7fb0716b82a0>

In [102]: reg7.match(test).group()
Out[102]: 'aaa'

In [103]: reg8 = re.compile('a{3,5}')

In [104]: reg8.match(test).group()
Out[104]: 'aaaaa'

Use case 2:


In [105]: reg6 = re.compile('a{1,2}?shique')

my expectation here is it should only give film1, but it working with both film1 and film2

In [107]: film1
Out[107]: 'ashique'

In [108]: film2
Out[108]: 'aashique'

In [109]: film3
Out[109]: 'aaashique'

In [110]: film4
Out[110]: 'shique'

In [111]: reg6.match(film1)
Out[111]: <_sre.SRE_Match at 0x7fb0716b8578>

In [112]: reg6.match(film1).group()
Out[112]: 'ashique'

In [113]: reg6.match(film2).group()
Out[113]: 'aashique'

Thanks,
santosh D
Reply all
Reply to author
Forward
0 new messages