Регулярное выражение C# для соответствия буквам, числам и подчеркиванию

Фантастика - тоже забыл окно просмотра! Fot all: просто добавьте

<meta name="viewport" content="width=device-width,initial-scale=1.0">

в свою голову

23
задан Brad Mace 9 July 2011 в 07:29
поделиться

3 ответа

РЕДАКТИРОВАТЬ:

@"^[a-zA-Z0-9\_]+$"

или

@"^\w+$"
36
ответ дан 29 November 2019 в 01:12
поделиться

Попробуйте поэкспериментировать с чем-нибудь вроде http://www.weitz.de/regex-coach / , который позволяет разрабатывать регулярное выражение в интерактивном режиме.

Он разработан для Perl, но помог мне понять, как регулярное выражение работает на практике.

3
ответ дан 29 November 2019 в 01:12
поделиться

@"^\w+$"

\w matches any "word character", defined as digits, letters, and underscores. It's Unicode-aware so it'll match letters with umlauts and such (better than trying to roll your own character class like [A-Za-z0-9_] which would only match English letters).

The ^ at the beginning means "match the beginning of the string here", and the $ at the end means "match the end of the string here". Without those, e.g. if you just had @"\w+", then "@@Foo@@" would match, because it contains one or more word characters. With the ^ and $, then "@@Foo@@" would not match (which sounds like what you're looking for), because you don't have beginning-of-string followed by one-or-more-word-characters followed by end-of-string.

22
ответ дан 29 November 2019 в 01:12
поделиться
Другие вопросы по тегам:

Похожие вопросы: