0

I've uploaded it here..

http://regex101.com/r/hC2eH3/1

Let email.body =

> From: "FastTech" <[email protected]>
>
> == Ship to ==
> Example Name
>
> Shipping via Registered Airmail w/Tracking
>
> == Shipping ETA ==
>
> == Items ordered ==
>
> == Other services ==
>
>
> == Order total ==
> $2.63
>
>
>
>
> Thank you again for choosing FastTech.
>
> Kind regards,
>
> The FastTech Team
>
>
> FastTech - gadget and electronics
> http://www.fasttech.com
> [email protected]
>
>
> This email was automatically generated for 

I can't get this to work in my script - using this code:

    regex = r'> == Ship to == *\n. (\w+\s\w+)'
    link = re.findall(regex, email.body)
    print link

print link just returns

[]

When it should be matching 'Example Name'

4
  • What's your expected output? Commented Oct 7, 2014 at 10:13
  • Your problem is in email.body. The regex works fine. Try printing the email.body. To see its real value Commented Oct 7, 2014 at 10:19
  • That regex works correctly here on a copy & pasted version of your email.body. So maybe you have string encoding issues that get magicked away when you post the email.body here.
    – PM 2Ring
    Commented Oct 7, 2014 at 10:25
  • Eg, your regex will have problems if any of those spaces are actually non-break space = \xa0, or if your end-of-line marker is \r or \r\n instead of \n.
    – PM 2Ring
    Commented Oct 7, 2014 at 10:31

1 Answer 1

1

Your regex works fine for me.

>>> import re
>>> s = """> From: "FastTech" <[email protected]>
... >
... > == Ship to ==
... > Example Name
... >
... > Shipping via Registered Airmail w/Tracking
... >
... > == Shipping ETA ==
... >
... > == Items ordered ==
... >
... > == Other services ==
... >
... >
... > == Order total ==
... > $2.63
... >
... >
... >
... >
... > Thank you again for choosing FastTech.
... >
... > Kind regards,
... >
... > The FastTech Team
... >
... >
... > FastTech - gadget and electronics
... > http://www.fasttech.com
... > [email protected]
... >
... >
... > This email was automatically generated for """
>>> m = re.findall(r'> == Ship to == *\n. (\w+\s\w+)', s)
>>> m
['Example Name']

OR

You may try this. A single space would match exactly a space where \s* would match zero or more spaces. So it's better to use \s* instead of a space.

>>> m = re.findall(r'^\s*>\s*==\s*Ship\s*to\s*==\s+>\s*(\w+\s\w+)', s, re.M)
>>> m
['Example Name']
2
  • This works... why doesn't mine? Thanks for the answer :-) it was my first attempt at a regex..
    – Andy
    Commented Oct 7, 2014 at 10:24
  • @Andy it may or maynot contain one or more spaces.. You're always welcome :-) Commented Oct 7, 2014 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.