Re: text processing problem



Matt wrote:
I'd HIGHLY suggest purchasing the excellent <a
href="http://www.oreilly.com/catalog/regex2/index.html";>Mastering
Regular Expressions</a> by Jeff Friedl.  Although it's mostly geared
towards Perl, it will answer all your questions about regular
expressions.  If you're going to work with regexs, this is a must-have.

That being said, here's what the new regular expression should be with
a bit of instruction (in the spirit of teaching someone to fish after
giving them a fish ;-)   )

my_expr = re.compile(r'(\w+)\s*(\(\1\))')

Note the "\s*", in place of the single space " ".  The "\s" means "any
whitespace character (equivalent to [ \t\n\r\f\v]).  The "*" following
it means "0 or more occurances".  So this will now match:

"there  (there)"
"there (there)"
"there(there)"
"there                                          (there)"
"there\t(there)" (tab)
"there\t\t\t\t\t\t\t\t\t\t\t\t(there)"
etc.

Hope that's helpful.  Pick up the book!

M@


Thanks again. I've read a number of tutorials on regular expressions but it's something that I hardly used in the past, so gone far too rusty.


Before my post, I've tried
my_expr = re.compile(r'(\w+) \s* (\(\1\))') instead but it doesn't work, so I'm a bit stumped......


Thanks again,
Maurice
.



Relevant Pages

  • Re: What do you need to have to be considered a Master at Perl?
    ... Someone who understands the Chomsky hierarchy, ... I don't think perl actually existed back then. ... "I've learnt how to match balanced parentheses with regular expressions." ... I don't care if perl regular expressions are or aren't kosher. ...
    (comp.lang.perl.misc)
  • Re: Regular expression help
    ... expression in english is a daunting task for me. ... Redefine your approach to regular expressions. ... Perl documentation, study the examples given (not only by ...
    (comp.lang.perl.misc)
  • Re: How to change the comparsion criteria of diff?
    ... I'm meeting the problem with diff methods in Perl. ... one containing regular-expression string ... normal regular expression does. ...
    (comp.lang.perl.misc)
  • Re: regular expression and not form
    ... How can I express "match any string except 'ABC'" in regular ... If you can use tools supporting Perl-compatible regex, like Perl, ...
    (comp.unix.shell)
  • Re: replace string with variable
    ... |> Hi, from Spain, I'm new user in perl, I have a problem with regular ... |> I dont know how I can replace the zeros on the right of the decimal ...
    (comp.lang.perl.misc)