[英]Strip special characters in front of the first alphanumeric character
I have a regex that picks up address from data.我有一个从数据中获取地址的正则表达式。 The regex looks for 'Address |
正则表达式查找“地址 | address |
地址 | etc.'
等等。' and picks up the characters following that until occurrence of 4 integers together.
并拾取后面的字符,直到一起出现 4 个整数。 This includes special characters, which need to be stripped.
这包括需要删除的特殊字符。
I run a loop to exclude all the special characters as seen in the code below.我运行一个循环来排除所有特殊字符,如下面的代码所示。 I need the code to drop only the special characters that are present in front of the first alphanumeric character.
我需要代码只删除出现在第一个字母数字字符前面的特殊字符。
Input (from OCR on an image):输入(来自图像上的 OCR):
Service address —_Unit8-10 LEWIS St, BERRI,SA 5343服务地址 —_Unit8-10 LEWIS St, BERRI,SA 5343
possible_addresses = list(re.findall('Address(.* \d{4}|)|address(.*\d{4})|address(.*)', data))
address = str(possible_addresses[0])
for k in address.split("\n"):
if k != ['(A-Za-Z0-9)']:
address_2 = re.sub(r"[^a-zA-Z0-9]+", ' ', k)
Got now:现在得到:
address : —_Unit 8 - 10 LEWIS ST, BERRI SA 5343地址:—_Unit 8 - 10 LEWIS ST, BERRI SA 5343
address_2 : Unit 8 10 LEWIS ST BERRI SA 5343地址_2 : Unit 8 10 LEWIS ST BERRI SA 5343
[\\W_]*
captures the special chars. [\\W_]*
捕获特殊字符。
import re
data='Service address —_Unit8-10 LEWIS St, BERRI,SA 5343'
possible_addresses = re.search('address[\W_]*(.*?\d{4})', data,re.I)
address = possible_addresses[1]
print('Address : ' address)
Address : Unit8-10 LEWIS St, BERRI,SA 5343
地址 : Unit8-10 LEWIS St, BERRI,SA 5343
I'm guessing that the expression we wish to design here should be swiping everything from address
to a four-digit zip, excluding some defined chars such as _
.我猜我们希望在这里设计的表达式应该是从
address
到四位 zip 的所有内容,不包括一些定义的字符,例如_
。 Let's then start with a simple expression with an i
flag, such as:然后让我们从一个带有
i
标志的简单表达式开始,例如:
(address:)[^_]*|[^_]*\d{4}
Any char that we do not want to have would go in here [^_]
.我们不想拥有的任何字符都将进入
[^_]
。 For instance, if we are excluding !
例如,如果我们排除
!
, our expression would become: ,我们的表达式会变成:
(address:)[^_!]*|[^_!]*\d{4}
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(address:)[^_]*|[^_]*\d{4}"
test_str = ("Address: Unit 8 - 10 LEWIS ST, BERRI SA 5343 \n"
"Address: Got now: __Unit 8 - 10 LEWIS ST, BERRI SA 5343\n"
"aDDress: Unit 8 10 LEWIS ST BERRI SA 5343\n"
"address: any special chars here !@#$%^&*( 5343\n"
"address: any special chars here !@#$%^&*( 5343")
matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.