Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

word containing numericals

Tags:

.net

regex

How can I recognize a word with regex which might contain numerics in it. So I want to capture "string1", "12inches", "log4net". But not 12/11/2011 or 18? Unfortunetly \b[\p{L}\d\p{M}]+\b grabs numericals too.

like image 434
Nickolodeon Avatar asked Nov 05 '22 10:11

Nickolodeon


1 Answers

This:

    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();
    } 

comes in mind.

"
\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
"
like image 74
FailedDev Avatar answered Nov 10 '22 19:11

FailedDev