6

For the past few hours I've been trying to match address(es) from the following sample data and I can't get it to work:

medicalHistory      None
address             24 Lewin Street, KUBURA, 
                NSW, Australia
email               MaryBeor@spambob.com


address             16 Yarra Street, 
                                     LAWRENCE, VIC, Australia
name                Mary   Beor
medicalHistory      None
phone               00000000000000000000353336907
birthday            26-11-1972

My plan was to find anything that starts with "address", is followed by any space followed by characters, numbers commas and newlines and ends with newline followed by a character. I came up with the following (and many variations of it):

address\s+([0-9a-zA-Z, \n\t]+)(?!\n\w)

Unfortunately that matches the following:

address             24 Lewin Street, KUBURA,
                NSW, Australia
email               MaryBeor  

and

address             16 Yarra Street,
                                 LAWRENCE, VIC, Australia
name                Mary   Beor
medicalHistory      None
phone               00000000000000000000353336907
birthday            26

instead of

address             24 Lewin Street, KUBURA, 
                NSW, Australia

and

address             16 Yarra Street,
                                 LAWRENCE, VIC, Australia

Can you please tell me what I'm doing wrong?

n1te
  • 945
  • 3
  • 11
  • 20

2 Answers2

5

I would do it this way:

address\s+((?![\r\n]+\w)[0-9a-zA-Z, \r\n\t])+

See it here on Regexr.

This ((?![\r\n]+\w)[0-9a-zA-Z, \r\n\t])+ is the important part, where I say, match the next character from [0-9a-zA-Z, \r\n\t], if (?![\r\n]+\w) is not following. This is matching what you expect.

In both your cases the regex stopped matching because of a character that is not included in your character class. If you want to go that way than you would need to combine a lazy quantifier and a positive lookahead:

address\s+([0-9a-zA-Z, \n\r\t]+?)(?=\r\w)

[0-9a-zA-Z, \n\r\t]+? is matching as less as possible till the condition (?=\r\w) is true.

See it here at Regexr

stema
  • 90,351
  • 20
  • 107
  • 135
  • @n1te Perhaps the greedy quantifier `+`, which consumes everything till the `@`, and still satisfy the negative look-ahead. – Rohit Jain Sep 24 '13 at 11:30
  • @n1te I added some explanation to my answer. – stema Sep 24 '13 at 11:42
  • 1
    @stema strange the second solution doesn't seem to work when I run it through preg_match but it works in Regexr – n1te Sep 24 '13 at 11:51
  • @n1te, I guess this is because I used only `\r` as newline character, since regexr has it. replace `\r` with `\n` or with `[\r\n]+` as I did in the first solution. – stema Sep 24 '13 at 11:56
  • both links gives no match – KVM Aug 04 '21 at 08:24
1

The problem with your regex is that + is greedy and goes until it finds a character out of that group, the @ in the first case and - in the second.

Another approach is to use a non-greedy quantifier and a positive look-ahead for a newline followed by a word-character, like ( version):

re.findall(r'address\s+.*?(?=\n\w)', s, re.DOTALL)

It yields:

['address             24 Lewin Street, KUBURA, \n                NSW, Australia',
 'address             16 Yarra Street, \n                                     LAWRENCE, VIC, Australia']
Birei
  • 35,723
  • 2
  • 77
  • 82
  • Thanks, that makes sense. I have to run your code to understand it better. Thanks for your help – n1te Sep 24 '13 at 11:37