社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

我的Python正则表达式在代码中工作不正常

Phiking • 3 年前 • 1432 次点击  

我有一个正则表达式,它适用于regex101。 但在我的python代码中,它没有返回任何值。 如果你们能帮我,那就太好了。

链接到我的正则表达式 https://regex101.com/r/T0Jvq4/1

这是我的职责。

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 ""

非常感谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129920
 
1432 次点击  
文章 [ 2 ]  |  最新文章 3 年前
anotherGatsby
Reply   •   1 楼
anotherGatsby    3 年前

你可以简化你的表达 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

Tim Biegeleisen
Reply   •   2 楼
Tim Biegeleisen    3 年前

我建议简化你的模式,只使用点所有模式:

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 ""