pattern searching

View: New views
9 Messages — Rating Filter:   Alert me  

pattern searching

by Ajith Gopinath :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

How to find out all the occuerence of a particular pattern like  in a long text where a capital letter in between two small letters ('aBa','dAd' etc..)
|| a j i t ||

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: pattern searching

by modulok-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

See the 're' module in the standard library. To quote the docs:

"This module ('re') provides regular expression matching operations
similar to those found in Perl. Both patterns and strings to be
searched can be Unicode strings as well as 8-bit strings."

You can find more information here: http://docs.python.org/library/re.html
-Modulok-

On 11/6/09, Ajith Gopinath <qbits143@...> wrote:
> Hi,
>
> How to find out all the occuerence of a particular pattern like  in a long
> text where a capital letter in between two small letters ('aBa','dAd' etc..)
> || a j i t ||
>
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: pattern searching

by Shashwat Anand :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Also you can try doing it using strings and this naive method:

>>> s = "aAabbnDeF"
>>> for i in range(1, len(s) - 1):
...     if s[i] in string.ascii_uppercase and s[i - 1] and s[i + 1] in string.ascii_lowercase:
...         print "".join([s[i - 1], s[i], s[i + 1]])
...
aAa
nDe

It simple look for all capital letters and verify whether it adjacent letters are small and print it.

On Sat, Nov 7, 2009 at 2:28 AM, Modulok <modulok@...> wrote:
See the 're' module in the standard library. To quote the docs:

"This module ('re') provides regular expression matching operations
similar to those found in Perl. Both patterns and strings to be
searched can be Unicode strings as well as 8-bit strings."

You can find more information here: http://docs.python.org/library/re.html
-Modulok-

On 11/6/09, Ajith Gopinath <qbits143@...> wrote:
> Hi,
>
> How to find out all the occuerence of a particular pattern like  in a long
> text where a capital letter in between two small letters ('aBa','dAd' etc..)
> || a j i t ||
>
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: pattern searching

by Bob Gailer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ajith Gopinath wrote:
> Hi,
>
> How to find out all the occuerence of a particular pattern like  in a
> long text where a capital letter in between two small letters
> ('aBa','dAd' etc..)

The other proposals are all good. However if performance is a concern
then I'd use string.maketran to create a translation table, then apply
it to the text using translate, such that all lower case letters are
translated to 'l', all upper case letters to 'u', then look for 'lul'.

import string
translationTable = string.maketrans(string.ascii_uppercase +
string.ascii_lowercase, 'u'*26 + 'l'*26)
translatedText = text.translate(translationTable)
start = 0
while True:
  start = translatedText.find('lul', start)
  if start >= 0:
    print text[start:start+3]
  else:
    break

Translate and find are both very fast.

--
Bob Gailer
Chapel Hill NC
919-636-4239
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: pattern searching

by Shashwat Anand :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

@Bob: the solution seems promising, and it's fast. Thanks for the improvement. However I would like to do a minor change to the code to prevent it going to infinite loop.

import string

text = raw_input()
translationTable = string.maketrans(string.ascii_uppercase + string.ascii_lowercase, 'u'*26 + 'l'*26)
translatedText = text.translate(translationTable)
start = 0
while True:
 start = translatedText.find('lul', start)
 if start >= 0:
  print text[start:start+3]
  start += 1
 else:
  break


On Sat, Nov 7, 2009 at 7:20 AM, bob gailer <bgailer@...> wrote:
Ajith Gopinath wrote:
Hi,

How to find out all the occuerence of a particular pattern like  in a long text where a capital letter in between two small letters ('aBa','dAd' etc..)

The other proposals are all good. However if performance is a concern then I'd use string.maketran to create a translation table, then apply it to the text using translate, such that all lower case letters are translated to 'l', all upper case letters to 'u', then look for 'lul'.

import string
translationTable = string.maketrans(string.ascii_uppercase + string.ascii_lowercase, 'u'*26 + 'l'*26)
translatedText = text.translate(translationTable)
start = 0
while True:
 start = translatedText.find('lul', start)
 if start >= 0:
  print text[start:start+3]
 else:
  break

Translate and find are both very fast.

--
Bob Gailer
Chapel Hill NC
919-636-4239

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: pattern searching

by Bob Gailer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Shashwat Anand wrote:

> @Bob: the solution seems promising, and it's fast. Thanks for the
> improvement. However I would like to do a minor change to the code to
> prevent it going to infinite loop.
>
> import string
>
> text = raw_input()
> translationTable = string.maketrans(string.ascii_uppercase +
> string.ascii_lowercase, 'u'*26 + 'l'*26)
> translatedText = text.translate(translationTable)
> start = 0
> while True:
>  start = translatedText.find('lul', start)
>  if start >= 0:
>   print text[start:start+3]
>   start += 1
>  else:
>   break
>
Good catch. My bad!

--
Bob Gailer
Chapel Hill NC
919-636-4239
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: pattern searching

by Ajith Gopinath :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks folks.

|| a j i t ||


On Sat, Nov 7, 2009 at 7:49 AM, bob gailer <bgailer@...> wrote:
Shashwat Anand wrote:
@Bob: the solution seems promising, and it's fast. Thanks for the improvement. However I would like to do a minor change to the code to prevent it going to infinite loop.

import string

text = raw_input()
translationTable = string.maketrans(string.ascii_uppercase + string.ascii_lowercase, 'u'*26 + 'l'*26)
translatedText = text.translate(translationTable)
start = 0
while True:
 start = translatedText.find('lul', start)
 if start >= 0:
 print text[start:start+3]
 start += 1
 else:
 break

Good catch. My bad!


--
Bob Gailer
Chapel Hill NC
919-636-4239
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: pattern searching

by Lie Ryan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ajith Gopinath wrote:

> Thanks folks.
>
> || a j i t ||
>
>
> On Sat, Nov 7, 2009 at 7:49 AM, bob gailer <bgailer@...
> <mailto:bgailer@...>> wrote:
>
>     Shashwat Anand wrote:
>
>         @Bob: the solution seems promising, and it's fast. Thanks for
>         the improvement. However I would like to do a minor change to
>         the code to prevent it going to infinite loop.
>

Be mindful that if the original string contains 'lul' you might miscount.

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: pattern searching

by Bob Gailer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Lie Ryan wrote:

> Ajith Gopinath wrote:
>> Thanks folks.
>>
>> || a j i t ||
>>
>>
>> On Sat, Nov 7, 2009 at 7:49 AM, bob gailer <bgailer@...
>> <mailto:bgailer@...>> wrote:
>>
>>     Shashwat Anand wrote:
>>
>>         @Bob: the solution seems promising, and it's fast. Thanks for
>>         the improvement. However I would like to do a minor change to
>>         the code to prevent it going to infinite loop.
>>
>
> Be mindful that if the original string contains 'lul' you might miscount.

I disagree. My algorithm works on a translation of the original. 'lul'
will be translated to 'lll'.



--
Bob Gailer
Chapel Hill NC
919-636-4239
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor