-4

How to check with more RegEx for one address in python using re.findall()

Ex: I want to apply the below regex rules

 # need to get address
    txt = "hello user 44 West 22nd Street, New York, NY 12345 from"    
    regexp = "[0-9]{1,3} .+, .+, [0-9]{5}"
    regexp1 = "[0-9]{1,3} .+, .+, [A-Z]{2} [0-9]{5}"
    regexp2 = "[0-9]{1,3} .+, .+, [A-Z]{2} [0-9]{9}"    
    re.findall(regexp, regexp1, regexp2, txt)

is this correct? I am getting error of this code

10
  • 1
    Are you sure that regular expressions are the appropriate tool for this job? Commented Dec 27, 2018 at 15:35
  • 2
    What is the error? Commented Dec 27, 2018 at 15:35
  • Perhaps more important, do any of those regex patterns even work on this input address string? Commented Dec 27, 2018 at 15:36
  • Did you take a look at manual? Commented Dec 27, 2018 at 15:36
  • If you are getting error, then that's probably not correct. What is the error message? What do you expect? Commented Dec 27, 2018 at 15:36

1 Answer 1

2

Finally I got answer from here

How to combine multiple regex into single one in python?

Working fine this

import re
re1 = r'\d+\.\d*[L][-]\d*\s[A-Z]*[/]\d*'
re2 = '\d*[/]\d*[A-Z]*\d*\s[A-Z]*\d*[A-Z]*'
re3 = '[A-Z]*\d+[/]\d+[A-Z]\d+'
re4 = '\d+[/]\d+[A-Z]*\d+\s\d+[A-z]\s[A-Z]*'

sentences = [string1, string2, string3, string4]
generic_re = re.compile("(%s|%s|%s|%s)" % (re1, re2, re3, re4)).findall(sentence)
Sign up to request clarification or add additional context in comments.

Comments

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.