Regular expression matches words without special characters or numbers

I am trying to figure out how to combine the word, but not the word1, w1ord, or any variation of the word! where is the "!" can be anything "#%!"

Basically, I want a match for a word that uses only AZ and az. The following does not work: /

([A-Za-z])\w+ 

Like "word% 5" is a match.

+1
source share
3 answers

You can do this with linq. See Table ASCII for letter values ​​from A to Z for characters. You can execute the top line after that only to check the range from 65 to 90.

 bool notOnlyLetters = yourStringValue.ToUpper().Any(x => !(x >= 65 && x <= 90)); 
+1
source

To match an entire string of English letters, use LINQ or regex:

 var hasAllEnglishLetters = x.All(c => (c >= 65 && c <= 90) || c >=97 && c<= 122)); var hasAllEnglishLetters = Regex.IsMatch(x, @"^[a-zA-Z]+$"); 

To combine words within a larger line, you can also use regex or LINQ approaches:

 var s = "Match word but not word1, w1ord or word!"; var res_linq = s.Split().Where(x => x.All(c => (c >= 65 && c <= 90) || c >=97 && c<= 122)); Console.WriteLine(string.Join(";", res_linq)); // REGEX var res_regex = Regex.Matches(s, @"(?<!\S)[a-zA-Z]+(?!\S)").Cast<Match>().Select(m=>m.Value); Console.WriteLine(string.Join(";", res_regex)); 

Watch the C # online demo

Details of the LINQ approach: with Split() string is broken into pieces of characters without spaces, and .All(c => (c >= 65 && c <= 90) || c >=97 && c<= 122) ensures that only these fragments that belong to the letters ASCII are selected (from 65 to 90 - capital letters ASCII, and 97 to 122 are lowercase).

Regex approach: (?<!\S) lookbehind does not match if there are no spaces before [a-zA-Z]+ (or the beginning of the line), 1 or more ASCII letters, and negative lookhhead (?!\S) does not match if there is no space (or end of line) after the letters.

+1
source

The following regular expression takes each "word" contained between:

  • beginning of line OR empty space (^|\s)

and

  • blank space OR end of line. (\s|$)

(^ | \ s) the word (\ S | $)

If you want to find every word consisting only of alphabetic characters, you can change the regular expression as follows:

(^ | \ s) [A-Za-Z] + (\ S | $)

+1
source

Source: https://habr.com/ru/post/1270067/


All Articles