简体   繁体   中英

Regex Expression to extract Address Street from a string

Given the sample texts, I want to extract Address Street (text between asterisk). Using the below Regex Expression, I'm able to extract Address Street for most of the sentences but mainly failing for text4 & text5.

regex = r"(^[0-9]+[\s\-0-9,A-Za-z]+)"
text1 = *9635 E COUNTY ROAD, 1000 N*.
text2 = *8032 LIBERTY RD S*.
text3 = *2915 PENNSYLVANIA AVENUE*  40 Other income (loss) 15 Alternative minimum tax (AMT) ilems
A 2,321
text4 = *2241 Western Ave*. 10 Other income loss 15 — Altemative minimum tax AMT itams
text5 = *450 7TH STREET, APT 2-M*
text6 = *9635 East County Road 1000 North*

My code---
for k,v in val.items():
 if k == "Shareholder Address Street":
   text = " ".join(v)
   pattern1 = r"(^[0-9]+[\s\-0-9,A-Za-z]+)"
   addressRegex = re.compile(pattern1)
   match = addressRegex.search(text)
   if match is not None:
      delta = []
      delta.append("".join(match.group(0)))
      val[k] = delta

Can anyone please suggest the change in the above regex as it is working fine for most of the documents?

Use

^\d+(?:[ \t][\w,-]+)*

See proof

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [ \t]                    any character of: ' ', '\t' (tab)
--------------------------------------------------------------------------------
    [\w,-]+                  any character of: word characters (a-z,
                             A-Z, 0-9, _), ',', '-' (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )*                       end of grouping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM