简体   繁体   中英

Regex - Match a string only when it contains any alphabetic characters

example strings

785*()&!~`a

##$%$~2343

455frt&*&*

i want to capture the first and the third but not the second since it doesnt contain any alphabet character plz help

In fact, I think [a-zA-Z] might suffice to match your strings.

To capture the whole thing, try: ^.*[a-zA-Z].*$

这是一种可能的方式:

.*[a-zA-Z]+

You should maybe clarify a bit what you mean by 'catpuring': do you want the whole string of just the ascii bits?

Also, you don't say if it should match just plain Roman alphabet (A to Z) or if it should also match Unicode chars to match strings in other languages.

If you just need to test your string, in C# you would do:

bool matching = Regex.IsMatch(myString, "[a-zA-Z]");

You wouldn't need anything else, since just one letter anywhere in the myString string will match (according to your definition).

If you want to match all letters (including non-ascii ones), use p{L} instead of [a-zA-Z] . See Unicode categories .

这是我最喜欢的RegEx测试网站: Javascript Regexp Tester和Cheat Sheet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM