Home > OS >  My Python Regex Expression is not working properly in my code
My Python Regex Expression is not working properly in my code

Time:03-25

I have this regex expression that work on regex101. But in my python code, it is returning None. If you guys can help me out that would be great.

Link to my regex expression https://regex101.com/r/T0Jvq4/1

This is my function.

legalDescription = tryRegexData(r"(?<=Legal Description:)(\n)(.*)^(?:(?!Property Use:).)*",propertyArea,0)

def tryRegexData(regex,area,num):

  try: 
      return re.search(regex,area.text).group(num).strip()
  except Exception as e:
      return ""

Thank you

CodePudding user response:

I would suggest simplifying your pattern and just use dot all mode:

def tryRegexData(area):

  try: 
      return re.search(r'\bLegal Description:\s (.*?)\s Property Use:', area.text, flags=re.S).group(1)
  except Exception as e:
      return ""

CodePudding user response:

You can simplify your rexpression to r"(?<=Legal Description:\n)(.*)(?=\nProperty Use:)"

import re

str = """General Info
Status: Certified
ACCOUNT
Property ID:
408123
Geographic ID:
Type:
P
Agent:
LETS GO TAX SERVICES - PO BOX 111
(Authorized)
Legal Description:
AREA THAT I WANT TO CAPTURE
Property Use:
OWNER
Name:
ABCD GAME SERVICE LLC
Secondary Name:
ATTN ACCTS PAYABLE
Mailing Address:
PO BOX 2222 NEW YORK NY 222515-12354
Owner ID:
123456"""

rgx  = r"(?<=Legal Description:\n)(.*)(?=\nProperty Use:)"

lst = re.findall(rgx, str)

print(lst[0])
# 'AREA THAT I WANT TO CAPTURE'

Try python Try regex

  • Related