简体   繁体   English

如何匹配字母和数字组合的单词,但避免只包含数字的单词

[英]How to match words with combination of letters and numbers but avoid words with only numbers

I need to create a RegEx that can get me the PO BOX full information from a string, I figured that I need to get and then everything else up to the point where a full string of numbers is found (the po box number is ok, after that , only if a word is a combination of numbers and letters is ok to match, if the whole word is made out of numbers then it should stop there ie 我需要创建一个RegEx,可以从字符串中获取PO BOX的全部信息,我认为我需要获取,然后再进行其他操作,直到找到完整的数字字符串为止(邮政信箱号是可以的, 之后 ,只有一个单词是数字和字母的组合才可以匹配,如果整个单词都是由数字组成的,那么它应该停在那里

From the following string: 从以下字符串:

"Po Box 321 Stn Commerce Court 123 Sample St"   

I need to get everything before 123 Sample St , like this: 我需要在123 Sample St之前获取所有信息,如下所示:

"Po Box 321 Stn Commerce Court "

From the following string: 从以下字符串:

"PO Box 456 Stn 1st Can Place" 

I need to get everything because there is no word made out of only numbers after the PO Box number 我需要得到所有东西,因为在邮政信箱号码之后,没有任何单词只能由数字组成

So far I achieve to get the first case working, but I haven't yet find an expression that works for both strings, 到目前为止,我实现了第一种情况的工作,但是我还没有找到一个适用于两个字符串的表达式,

My attempt: 我的尝试:

Regex.Match(txtString.Text, @"PO BOX [0-9]+([\s\w][^0-9]*|\s[a-zA-Z0-9]\s)", RegexOptions.IgnoreCase)

Thank you 谢谢

You can use a positive look ahead : 您可以使用正面的眼光:

PO BOX [0-9]+.*(?= \d+|$)

See demo https://regex101.com/r/bK7nY2/1 参见演示https://regex101.com/r/bK7nY2/1

Or use a negative look ahead : 或使用负面的展望:

PO BOX [0-9]+((?!\d+ ).)*

Demo https://regex101.com/r/bK7nY2/3 演示https://regex101.com/r/bK7nY2/3

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

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