1

整个订单可享受 20% 的折扣和免费送货(仅限首次购买的客户)!在结帐时输入优惠券代码 NEW20VISION。限时优惠。

我从 db 中得到这种类型的字符串。我必须找出字符串中是否有任何字母数字词。如果它包含任何字母数字词。我已经下划线了。就像上面的情况一样:NEW20VISION

4

4 回答 4

1

Raphaël Althaus 是对的,但我认为最好为它添加一些被动组,以避免获得无用的匹配

用于测试的字符串:

Get 20% Off Your Entire Order & FREE Shipping (first55 time55 custo55mers only)! Enter coudf45pon code NEW20VISION at checkout. Limited time offer.

Raphaël Althaus 的正则表达式:

\b[a-zA-Z\d]*(([a-zA-Z]+\d+)|(\d+[a-zA-Z+]))[a-zA-Z\d]*\b

我的正则表达式:

\b[a-zA-Z\d]*(?:(?:[a-zA-Z]+\d+)|(?:\d+[a-zA-Z+]))[a-zA-Z\d]*\b

Raphaël Althaus 的正则表达式结果:

 ===next match===
Group[0]: first55
Group[1]: t55
Group[2]: t55
Group[3]: 
===next match===
Group[0]: time55
Group[1]: e55
Group[2]: e55
Group[3]: 
===next match===
Group[0]: custo55mers
Group[1]: 5m
Group[2]: 
Group[3]: 5m
===next match===
Group[0]: coudf45pon
Group[1]: 5p
Group[2]: 
Group[3]: 5p
===next match===
Group[0]: NEW20VISION
Group[1]: 0V
Group[2]: 
Group[3]: 0V

我的正则表达式的结果:

 ===next match===
    Group[0]: first55
    ===next match===
    Group[0]: time55
    ===next match===
    Group[0]: custo55mers
    ===next match===
    Group[0]: coudf45pon
    ===next match===
    Group[0]: NEW20VISION
于 2012-05-14T12:22:36.530 回答
1

使用以下正则表达式 /[a-zA-Z0-9]/

或查看链接:http ://www.dreamincode.net/code/snippet5818.htm

于 2012-05-14T11:50:34.820 回答
0

How about this:

Regex regexObj = new Regex(@"\w*(?:\d\p{L}|\p{L}\d)\w*");

This matches an alphanumeric word that contains at least one digit followed by a letter or vice versa.

The only "wart" is that \w also matches the underscore. If you don't want that, it gets slighty more ugly:

Regex regexObj = new Regex(@"[\d\p{L}]*(?:\d\p{L}|\p{L}\d)[\d\p{L}]*");
于 2012-05-14T14:45:02.450 回答
0

好吧,我认为有点混乱,因为您搜索字母和数字词(“20”、“20ab”、“test”被视为字母数字)。

这个应该可以工作(可以简化,我很确定......)

用 Regex.Replace 测试,NEW20VISION 不在你的字符串中。

 var regex = @"\b[a-zA-Z\d]*(([a-zA-Z]+\d+)|(\d+[a-zA-Z+]))[a-zA-Z\d]*\b";
 var text = "Get 20% Off Your Entire Order & FREE Shipping (first time customers only)! Enter coupon code NEW20VISION at checkout. Limited time offer.";

 var text2 = "20 test 1020test test2010, test10 20";


 var t2 = Regex.Replace(text, regex, string.Empty); 
 var t3 = Regex.Replace(text2, regex, string.Empty);
于 2012-05-14T12:17:06.920 回答