包含数字的单词

时间:2011-11-29 16:37:45

标签: .net regex

如何识别可能包含数字的正则表达式的单词。 所以我想捕获“string1”,“12inches”,“log4net”。但不是12/11/2011或18? 不幸的是\b[\p{L}\d\p{M}]+\b也抓住了数字。

2 个答案:

答案 0 :(得分:2)

此:

    Regex regexObj = new Regex(@"\b(?=\S*[a-z])\w+\b", RegexOptions.IgnoreCase);
    Match matchResults = regexObj.Match(subjectString);
    while (matchResults.Success) {
        // matched text: matchResults.Value
        // match start: matchResults.Index
        // match length: matchResults.Length
        matchResults = matchResults.NextMatch();
    } 

考虑到了。

"
\b          # Assert position at a word boundary
(?=         # Assert that the regex below can be matched, starting at this position (positive lookahead)
   \S          # Match a single character that is a “non-whitespace character”
      *           # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   [a-z]       # Match a single character in the range between “a” and “z”
)
\w          # Match a single character that is a “word character” (letters, digits, etc.)
   +           # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\b          # Assert position at a word boundary
"

答案 1 :(得分:0)

您想要将单词中的字母和数字匹配吗?这应该有效:\b(\w+\d+|\d+\w+)[\w\d]+\b