简体   繁体   English

正则表达式以匹配字符串,空格或逗号的开头

[英]Regex to match start of string or space or comma

I am looking to match a postcode using the following regex: 我正在寻找使用以下正则表达式来匹配邮政编码:

(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})

I am trying to parse an address from an HTML document and as such I only want to match nodes that start with a postcode or contain a postcode that is preceded by a space or comma. 我试图解析HTML文档中的地址,因此,我只想匹配以邮政编码开头或包含以空格或逗号开头的邮政编码的节点。 Otherwise there are too many false positives eg matching colours (preceded by #). 否则,会有太多的误报,例如匹配的颜色(以#开头)。

I need to amend the regex to either find the postcode with no preceding characters or a space or comma immediately preceding it and any number of characters before this. 我需要对正则表达式进行修改,以查找没有前一个字符的邮政编码,或者在前一个空格或逗号之前加上任意数量的字符。 How can I do this? 我怎样才能做到这一点?

For example, I would want to match: 例如,我要匹配:

IP14 2PL
1 The street, ipswich, IP14 2PL
1 The street, ipswich,IP14 2PL

BUT NOT 但不是

https://t.co/ip142plzruc

Just add this in front of your expression: 只需在表达式前面添加以下内容:

(?:^|[, ])

It will make mandatory to have a space or a comma before or it begins a line. 强制或在一行开始之前必须有空格或逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM