如何使用正则表达式从字符串中查找字母数字单词? [英] How to find alphanumeric word from a string using regular expression?

查看:213
本文介绍了如何使用正则表达式从字符串中查找字母数字单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部订单享受八折优惠免费送货(仅限首次客户)!在结账时输入优惠码NEW20VISION。限时优惠。



Quote:

我从db获得这种类型的字符串。我有找出字符串中是否有任何字母数字。如果它包含任何字母数字字。我有下划线。在上面的例子中它是:NEW20VISION

解决方案

试试这个



  string  s = TextBox1。文本; 
string str = ;
string [] words = s.Split(' ');
foreach 字符串 单词)
{
Regex r = new 正则表达式( [A-ZA-Z]);

if (r.IsMatch(word))
{
Regex r1 = new 正则表达式( [0-9]);
if (r1.IsMatch(word))
{
str = str + + < U> + word + < / U>;
}
else
str = str + + + word + ;
}
else
str = str + + + word + ;
}

Label1.Text = str;





希望这有帮助,如果是,那么接受和投票给答案否则回复你的查询。

--RDBurmon Sr.Software工程师


RDBurmon 给出的解决方案1是好的。



作为替代方案,可以使用以下正则表达式来搜索字母数字单词。它首先搜索具有2个或更多字母数字字符的单词,然后检查搜索到的单词中是否存在至少一个数字和至少一个字符,并且只有在这样的情况下,该单词才会被捕获为一个组。 br />


此外,捕获的组可用于替换。例如。每个捕获的单词可以包含在< u>中。和< / u>如下图所示:



正则表达式可在此处测试 http://regexhero.net/tester/ [ ^ ]

  string  inputText =  @  全部订单享受八折优惠并免费送货 + 
@ (仅限首次使用客户)!在结账时输入优惠券代码NEW20VISION。 +
@ 限时优惠.NEW20 20VISION New20New20New20Ne20 N2;

MatchCollection alphaNumMatches = Regex.Matches(inputText,
@ (?= \ w {2,})(?= \ w * \ d +)(?= \ w * \p {L} +)(\w {2,})
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

foreach (匹配匹配 alphaNumMatches){
if (match.Groups.Count > 0
Console.WriteLine(match.Groups [ 1 ]。Value);
}

string replacementText = Regex.Replace(inputText,
@ (?= \w {2,})(?= \ w * \d +)(?= \ w * \p {L } +)(\w {2,})
@ < U>

1·; / U>中);

Console.WriteLine(replacementText);

// 输出

// NEW20VISION
// NEW20
// 20VISION
// New20New20New20Ne20
// N2
// 获取20折扣整个订单和折扣免费送货(仅限首次客户)!输入优惠券代码< u> NEW20VISION< / u>在结账时。限时优惠。 < U> NEW20< / U> < U> 20VISION< / U> < U> New20New20New20Ne20< / U> < U> N2< / U>


Get 20% Off Your Entire Order & FREE Shipping (first time customers only)! Enter coupon code NEW20VISION at checkout. Limited time offer.

Quote:

I am getting this type of string from db.I have to find out if there is any alphanumeric word in the string.If it contains any alphanumeric word.I have underline it.Like in the above case it is:NEW20VISION

解决方案

Try this

string s = TextBox1.Text;
string str = "";
string[] words = s.Split(' ');
foreach (string word in words)
{
    Regex r = new Regex("[a-zA-Z]");

    if (r.IsMatch(word))
    {
        Regex r1 = new Regex("[0-9]");
        if (r1.IsMatch(word))
        {
          str=str +  " " + "<U>" +  word + "</U>";
        }
        else
            str=str +  " " + "" +  word + "";
    }
    else
            str=str +  " " + "" +  word + "";
}

Label1.Text=str;



Hope this helps if yes then accept and vote the answer otherwise revert back with your queries.
--RDBurmon Sr.Software Engineer


The Solution 1 given by RDBurmon is good.

As an alternative the following regular expression can be used to search for the alpha numeric words. It first searches for a word with 2 or more alpha numeric characters, then it checks whether at least one digit and at least one character is present in the searched word and only if it is so, the word will be captured as a group.

Further the captured group can be used for replacement. For eg. each of the captured word can be enclosed within <u> and </u> as shown below:

The Regular Expression can be tested here http://regexhero.net/tester/[^]

string inputText = @"Get 20% Off Your Entire Order & FREE Shipping " +
       @"(first time customers only)! Enter coupon code NEW20VISION at checkout." +
       @"Limited time offer. NEW20 20VISION  New20New20New20Ne20  N2";

MatchCollection alphaNumMatches = Regex.Matches(inputText,
       @"(?=\w{2,})(?=\w*\d+)(?=\w*\p{L}+)(\w{2,})",
       RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

foreach(Match match in alphaNumMatches){
    if (match.Groups.Count > 0)
        Console.WriteLine (match.Groups[1].Value);
}

string replacedText = Regex.Replace(inputText,
       @"(?=\w{2,})(?=\w*\d+)(?=\w*\p{L}+)(\w{2,})",
       @"<u>


1</u>"); Console.WriteLine (replacedText); //Output //NEW20VISION //NEW20 //20VISION //New20New20New20Ne20 //N2 //Get 20% Off Your Entire Order & FREE Shipping (first time customers only)! Enter coupon code <u>NEW20VISION</u> at checkout.Limited time offer. <u>NEW20</u> <u>20VISION</u> <u>New20New20New20Ne20</u> <u>N2</u>


这篇关于如何使用正则表达式从字符串中查找字母数字单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆