2011-11-29 11 views
5

Come posso riconoscere una parola con regex che potrebbe contenere numeri in essa contenuti. Quindi voglio catturare "string1", "12inches", "log4net". Ma non il 12/11/2011 o il 18? Unfortunetly \b[\p{L}\d\p{M}]+\b afferra anche numeri.parola contenente numeri

risposta

2

questo:

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

viene in mente.

" 
\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 
" 
+0

Grazie. In realtà ho un problema un po 'più difficile: devo riconoscere le frasi per spazi o trattini e avere questo: (? <= \ B ([\ p {L} \ p {M}] + | \ s) \ b) [\ s \ p {} Pd \ s] + (? = \ b [\ p {} L \ p {M}] + \ b). Parentesi sinistra e destra significa qualche parola (può avere dieresi). Ora vedo che hai inserito anche qualche riferimento futuro. – Nickolodeon

+0

@Nickolodeon Penso che dovresti modificare la tua domanda in modo appropriato perché la mia risposta risponde a questa domanda. Si prega di inviare alcuni esempi appropriati di input/output in modo che possiamo aiutare. – FailedDev

+0

Beh, scusa, volevo semplificare la domanda, quindi ho chiesto solo una parte di esso, immagino. Ho bisogno di 1) Robocop - 3 => Robocop3. 2) Hello 2 => Hello2 3) Hello world => Helloworld. Ciò significa rimuovere spazi o trattini parole se nessuno degli adiacenti è un numero o una data. – Nickolodeon

0

Vuoi abbinare una parola con lettere e numeri al suo interno? Questo dovrebbe funzionare: \b(\w+\d+|\d+\w+)[\w\d]+\b.

Problemi correlati