【发布时间】:2021-04-08 03:19:13
【问题描述】:
我正在使用正则表达式从地址字符串中提取字母“u”,但仅当它用作缩写时(u、u.、U、U. 等)。但是,我正在运行的问题是我拥有的字符串列表很乱并且充满了错误。我已经尝试从我在数据中看到的各种错误中提取我需要的东西。我知道我一定遗漏了一些小东西,但感谢您提供任何帮助。
我已经尝试过这些正则表达式:
- (\s(u|U)?.?,?.?\s)
- [^\w+][uU]
- [^\w+][uU][^tca]
我还有另一个解决此问题的想法,这需要拆分地址(在街道、号码等之间拆分),然后修复街道部分并将其重新粘合在一起。我有一些运气实际上只是将数字部分拉出来:
- (\d+-\d+|\d+/*\w*|(-))
但是,我想看看我在应该选择“u”的正则表达式中哪里搞砸了。 Regex101.com 一直是我最好的朋友,如果没有它,我不会走到这一步。
test_strings = [
"Holics u 5/a",
"Holics U 5/a",
"Holics u5/a",
"Huolics u 5/a",
"Holics u. 5/a",
"Holuics u5",
"Holics and other stuff u more stuff after 5",
"Houlics utca 5"
]
# two regex patterns I have considered
print("First regex pattern ------------------------------------")
pattern = r"[^\w+][uU]"
replacement_text = " utca "
for item in test_strings:
print(re.sub(pattern,replacement_text,item))
print("\nSecond regex pattern ------------------------------------")
pattern = r"[^\w+][uU][^tca]"
replacement_text = " utca "
for item in test_strings:
print(re.sub(pattern,replacement_text,item))
以上代码的结果:
第一个正则表达式模式:
Holics utca 5/a
Holics utca 5/a
Holics utca 5/a
Huolics utca 5/a
Holics utca . 5/a
Holuics utca 5
Holics and other stuff utca more stuff after 5
Houlics utca tca 5 # <-------------------------------- issue
第二个正则表达式模式:
Holics utca 5/a
Holics utca 5/a
Holics utca /a # <----------------------------------- issue
Huolics utca 5/a
Holics utca 5/a
Holuics utca <-------------------------------------- issue
Holics and other stuff utca more stuff after 5
Houlics utca 5
除了第一个正则表达式模式中的最后一行(“Houlics utca tca 5”)外,一切正常,当我尝试创建一个表达式来考虑包含“utca”的字符串时,我丢失了字符串中的数字比如“Holics u5/a”。
在大多数情况下,我希望结果是:
- 你好。 5/a -----> Holics utca 5/a
作为最后一点,我有删除句点和空格的函数。
【问题讨论】:
-
试试
re.sub(r'\b[uU](?=\b|\d)\.?\s*', 'utca ', s),见regex101.com/r/WgFPl5/1 -
投票结束,因为这里有多个问题:1)避免吞下数字2)有条件地插入空格。
-
这个问题绝不是对这些问题的欺骗。