简体   繁体   English

去除第一个字母数字字符前面的特殊字符

[英]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}

Demo演示

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}

Test测试

# 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.

相关问题 正则表达式将字母数字与特殊字符匹配 - Regex matching alphanumeric with special characters 特殊字符上的python strip函数 - python strip function on special characters 如何剪切第一个字符直到找到特殊字符? - How to cut first characters until a special character found? 在 python 中使用正则表达式中的 lookbehnd 提取字符串直到 '\n' 字符。 它也可以有字母数字字符、特殊字符和空格 - extract strings till ' \n ' character using lookbehnd in regex in python. And it can have alphanumeric characters, special characters and space also 在特殊字符前添加空格 - Adding whitespace in front of special characters 提取两个特殊字符之间的字母数字字符串 - Extract an alphanumeric string between two special characters 从Unicode字符串中去除特殊字符和标点符号 - Strip special characters and punctuation from a unicode string 在之前的任何位置查找具有字母数字字符的非字母数字字符 - Find non-alphanumeric characters with alphanumeric character anywhere before 如何仅获取字符串中任何特殊字符之前的字母数字字符? - How to get only the alphanumeric characters that are before any special characters in a string? 想要使用python regex从某些特殊字符中提取字母数字文本 - Want to extract the alphanumeric text with certain special characters using python regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM