Re: Regexp problem

2009-07-30 Thread Peter Brett
Marcus Wanner  writes:

> On 7/30/2009 9:32 AM, Beldar wrote:
>> On 30 jul, 15:07, MRAB  wrote:
>>> Beldar wrote:
>>>> Hi there!
>>>> I have a problem and i'm not very good at regular expressions.
>>>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri
>>>> lalala" I need a regexp to get the 'beldar' part, the format is
>>>> 'something-is-listening', i need to get the something part, use it in
>>>> my code, and then replace the whole 'something-is-listening' for
>>>> another string.
>>> \w+ will match a word and enclosing it in (...) will capture what was
>>> matched:
>>>
>>>  m = re.search(r"(\w+)-is-listening", text)
>>>  print "Captured '%s'" % m.group(1)
>>>  print "Matched from %d to %d" % (m.start(), m.end())
>>
>> Ok, thank you all, it was very helpful!
> Wow, I really need to learn more about regexp...
> Any tutorials you guys can recommend?

I have to confess that after fiddling with regexps for quite a while
with no great success, I learnt the hard (and best) way, i.e. using
them to write something vile and horrible. [*] I commend this path to
you also. ;-)

Cheers,

  Peter

[*] http://git.gpleda.org/?p=gaf.git;a=blob;f=libgeda/desktop-i18n;h=6fab9b85b

-- 
Peter Brett 
Remote Sensing Research Group
Surrey Space Centre
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extract the numeric and alphabetic part from an alphanumeric string

2009-08-03 Thread Peter Brett
Sandhya Prabhakaran  writes:

> Hi,
>
> I have a string as str='123ACTGAAC'.
>
> I need to extract the numeric part from the alphabetic part which I
> did using
>>>>numer=re.findall(r'\d+',str)
>>>>numer
> 123
>
> To get the alphabetic part, I could do
>>>>alpha=str.replace('123','')
>>>>alpha
> ACTGAAC
> But when I give
>>>>alpha=str.replace(numer,'')
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: expected a character buffer object
>
> How do I blank out the initial numeric part so as to get just the
> alphabetic part. The string is always in the same format.

Firstly, you really should read the Regular Expression HOWTO:

  http://docs.python.org/howto/regex.html#regex-howto

Secondly, is this what you wanted to do?

  >>> p = re.compile(r'^\d+')
  >>> p.sub('', '123ACTGAAC')
  'ACTGAAC'

Regards,

   Peter

-- 
Peter Brett 
Remote Sensing Research Group
Surrey Space Centre
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: expandtabs acts unexpectedly

2009-08-19 Thread Peter Brett
"digisat...@gmail.com"  writes:

> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> ' test\ttest'.expandtabs(4)
> ' test   test'
>>>> 'test \ttest'.expandtabs(4)
> 'testtest'
>
> 1st example: expect returning 4 spaces between 'test', 3 spaces
> returned
> 2nd example: expect returning 5 spaces between 'test', 4 spaces
> returned
>
> Is it a bug or something, please advice.

Consider where the 4-space tabstops are relative to those strings:

 test   test
testtest
^   ^   ^

So no, it's not a bug.

If you just want to replace the tab characters by spaces, use:

  >>> " test\ttest".replace("\t", "")
  ' testtest'
  >>> "test \ttest".replace("\t", "")
  'test test'

HTH,

   Peter

-- 
Peter Brett 
Remote Sensing Research Group
Surrey Space Centre
-- 
http://mail.python.org/mailman/listinfo/python-list