Re: Python re expr from Perl to Python

2007-01-07 Thread Florian Diesch
Michael M. [EMAIL PROTECTED] wrote:

 In Perl, it was:


   ## Example: Abc | def | ghi | jkl
   ##   - Abc ghi jkl
   ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
 pipe).
   $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;

   ## -- remove [ and ] in text
   $na =~ s/\[//g;
   $na =~ s/\]//g;
   # print DEB: \$na\\n;


 # input string
 na=Abc | def | ghi | jkl [gugu]
 # output
 na=Abc ghi jkl gugu


 How is it done in Python?

 import re
 na=Abc | def | ghi | jkl [gugu]
 m=re.match(r'(\w+ )\| (\w+ )\| (\w+ )\| (\w+ )\[(\w+)\]', na)
 na=m.expand(r'\1\2\3\5')
 na
'Abc def ghi gugu'



   Florian
-- 
http://www.florian-diesch.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python re expr from Perl to Python

2007-01-07 Thread Thomas Ploch
Florian Diesch schrieb:
 Michael M. [EMAIL PROTECTED] wrote:
 
 In Perl, it was:


   ## Example: Abc | def | ghi | jkl
   ##   - Abc ghi jkl
   ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
 pipe).
   $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;

   ## -- remove [ and ] in text
   $na =~ s/\[//g;
   $na =~ s/\]//g;
   # print DEB: \$na\\n;


 # input string
 na=Abc | def | ghi | jkl [gugu]
 # output
 na=Abc ghi jkl gugu


 How is it done in Python?
 
 import re
 na=Abc | def | ghi | jkl [gugu]
 m=re.match(r'(\w+ )\| (\w+ )\| (\w+ )\| (\w+ )\[(\w+)\]', na)
 na=m.expand(r'\1\2\3\5')
 na
 'Abc def ghi gugu'

I'd rather have the groups grouped without the whitespaces

  import re
  na=Abc | def | ghi | jkl [gugu]
  m=re.match(r'(\w+) \| (\w+) \| (\w+) \| (\w+) \[(\w+)\]', na)
  na=m.expand(r'\1 \3 \4 \5')
  na
 'Abc ghi jkl gugu'

Thomas

-- 
http://mail.python.org/mailman/listinfo/python-list


Python re expr from Perl to Python

2007-01-06 Thread Michael M.

In Perl, it was:


   ## Example: Abc | def | ghi | jkl
   ##   - Abc ghi jkl
   ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st 
pipe).
   $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;

   ## -- remove [ and ] in text
   $na =~ s/\[//g;
   $na =~ s/\]//g;
   # print DEB: \$na\\n;


# input string
na=Abc | def | ghi | jkl [gugu]
# output
na=Abc ghi jkl gugu


How is it done in Python?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python re expr from Perl to Python

2007-01-06 Thread Jorge Godoy
Michael M. [EMAIL PROTECTED] writes:

 In Perl, it was:


   ## Example: Abc | def | ghi | jkl
   ##   - Abc ghi jkl
   ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st pipe).
   $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;

   ## -- remove [ and ] in text
   $na =~ s/\[//g;
   $na =~ s/\]//g;
   # print DEB: \$na\\n;


 # input string
 na=Abc | def | ghi | jkl [gugu]
 # output
 na=Abc ghi jkl gugu


 How is it done in Python?

The simplest form:

 na=Abc | def | ghi | jkl [gugu]
 na_out = na.replace('def', '').replace(' | ', ' ').replace('  ', ' 
 ').replace('[', '').replace(']', '').strip()
 na_out
'Abc ghi jkl gugu'
 


Another form:

 na_out = ' '.join(na.split(' | ')).replace('[', '').replace(']', 
 '').replace(' def', '')
 na_out
'Abc ghi jkl gugu'
 


There is the regular expression approach as well as several other
alternatives.  I could list other (simpler, more advanced, etc.) alternatives,
but you can also play with Python by yourself.  If you have a more concrete
specification, send it to the group.



-- 
Jorge Godoy  [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python re expr from Perl to Python

2007-01-06 Thread Carsten Haese
On Sat, 2007-01-06 at 15:43 +0100, Michael M. wrote:
 In Perl, it was:
 
 
## Example: Abc | def | ghi | jkl
##   - Abc ghi jkl
## Take only the text betewwn the 2nd pipe (=cut the text in the 1st 
 pipe).
$na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;
 
## -- remove [ and ] in text
$na =~ s/\[//g;
$na =~ s/\]//g;
# print DEB: \$na\\n;
 
 
 # input string
 na=Abc | def | ghi | jkl [gugu]
 # output
 na=Abc ghi jkl gugu
 
 
 How is it done in Python?

Here's an almost literal translation:

##
import re
na = re.sub(r\ \|(.*?)\ \|(.*?)\ \|, r\2, na)
na = na.replace([, )
na = na.replace(], )
##

Background information on regular expressions in Python can be found
here:

http://www.amk.ca/python/howto/regex/
http://docs.python.org/lib/module-re.html

Hope this helps,

Carsten.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python re expr from Perl to Python

2007-01-06 Thread Paddy

Michael M. wrote:

 In Perl, it was:


## Example: Abc | def | ghi | jkl
##   - Abc ghi jkl
## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
 pipe).
$na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;

## -- remove [ and ] in text
$na =~ s/\[//g;
$na =~ s/\]//g;
# print DEB: \$na\\n;


 # input string
 na=Abc | def | ghi | jkl [gugu]
 # output
 na=Abc ghi jkl gugu


 How is it done in Python?

Here is how to do it without regexps in python.
The first and last line below are all that are needed. The others show
intermediate expressions that lead to the result.

 from itertools import groupby

 na=Abc | def | ghi | jkl [gugu]
 [(g[0], ''.join(g[1])) for g in groupby(na, lambda c: c not in ' \t|[]')]
[(True, 'Abc'), (False, ' | '), (True, 'def'), (False, ' | '), (True,
'ghi'), (False, ' | '), (True, 'jkl'), (False, ' ['), (True, 'gugu'),
(False, ']')]
 [''.join(g[1]) for g in groupby(na, lambda c: c not in ' \t|[]') if g[0]]
['Abc', 'def', 'ghi', 'jkl', 'gugu']

 ' '.join(''.join(g[1]) for g in groupby(na, lambda c: c not in ' \t|[]') if 
 g[0])
'Abc def ghi jkl gugu'
 


- Paddy.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python re expr from Perl to Python

2007-01-06 Thread Paddy

Paddy wrote:

 Michael M. wrote:

  In Perl, it was:
 
 
 ## Example: Abc | def | ghi | jkl
 ##   - Abc ghi jkl
 ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
  pipe).
 $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;
 
 ## -- remove [ and ] in text
 $na =~ s/\[//g;
 $na =~ s/\]//g;
 # print DEB: \$na\\n;
 
 
  # input string
  na=Abc | def | ghi | jkl [gugu]
  # output
  na=Abc ghi jkl gugu
 
 
  How is it done in Python?

 Here is how to do it without regexps in python.
 The first and last line below are all that are needed. The others show
 intermediate expressions that lead to the result.

  from itertools import groupby

  na=Abc | def | ghi | jkl [gugu]
  [(g[0], ''.join(g[1])) for g in groupby(na, lambda c: c not in ' \t|[]')]
 [(True, 'Abc'), (False, ' | '), (True, 'def'), (False, ' | '), (True,
 'ghi'), (False, ' | '), (True, 'jkl'), (False, ' ['), (True, 'gugu'),
 (False, ']')]
  [''.join(g[1]) for g in groupby(na, lambda c: c not in ' \t|[]') if g[0]]
 ['Abc', 'def', 'ghi', 'jkl', 'gugu']

  ' '.join(''.join(g[1]) for g in groupby(na, lambda c: c not in ' \t|[]') 
  if g[0])
 'Abc def ghi jkl gugu'
 


 - Paddy.

And I leave the deletion of def to the reader :-)

(i.e: I missed that bit and adding it in would make a long
comprehension too long to comprehend).

-- 
http://mail.python.org/mailman/listinfo/python-list