1

I'm trying to figure out how to match "word" but not "word1", "w1ord" or any variation of "word!" where "!" could be anything "#%!"

Basically I want to match against word that only uses A-Z and a-z. The following does not work :/

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

As "word%5" is a match.

Jason94
  • 13,320
  • 37
  • 106
  • 184

3 Answers3

1

You can do it with linq. Look the ASCII Table to see the letter values from A to Z for chars. You can Upper your string after that only to check from range 65 to 90.

bool notOnlyLetters = yourStringValue.ToUpper().Any(x => !(x >= 65 && x <= 90));
mybirthname
  • 17,949
  • 3
  • 31
  • 55
1

To match a whole string consisting 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 match words inside a larger string, you may either use a regex or LINQ approaches, too:

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

See the online C# demo

LINQ approach details: With Split(), the string is split into chunks of non-whitespace symbols and .All(c => (c >= 65 && c <= 90) || c >=97 && c<= 122) makes sure only those chunks are fetched that belong to ASCII letters (65 to 90 - uppercase ASCII letters, and 97 to 122 are lowercase ones).

Regex approach: the (?<!\S) lookbehind fails the match if there is no whitespace before [a-zA-Z]+ (or start of string), 1 or more ASCII letters, and the negative lookahead (?!\S) fails the match if there is no whitespace (or end of string) after the letters.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

The following regex accepts every "word" occurrence contained between:

  • the start of a line OR an empty space (^|\s)

and

  • an empty space OR the end of a line. (\s|$)

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

If you want to find every word composed by alphabetic characters only, you can change the regex as follows:

(^|\s)[a-zA-Z]+(\s|$)

Fabrizio
  • 7,603
  • 6
  • 44
  • 104