简体   繁体   English

正则表达式 - 仅在字符串包含任何字母字符时匹配字符串

[英]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 我想捕获第一个和第三个但不是第二个,因为它不包含任何字母字符plz帮助

In fact, I think [a-zA-Z] might suffice to match your strings. 事实上,我认为[a-zA-Z]可能足以匹配你的字符串。

To capture the whole thing, try: ^.*[a-zA-Z].*$ 要捕捉整个事物,请尝试: ^.*[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? 你应该澄清一下'catpuring'的意思:你想要整个字符串只是ascii位吗?

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. 此外,您不会说它是否应该匹配普通的罗马字母(A到Z),或者它是否也应该匹配Unicode字符以匹配其他语言中的字符串。

If you just need to test your string, in C# you would do: 如果你只需要测试你的字符串,在C#中你会这样做:

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). 你不需要任何其他东西,因为myString字符串中任何地方只有一个字母匹配(根据你的定义)。

If you want to match all letters (including non-ascii ones), use p{L} instead of [a-zA-Z] . 如果要匹配所有字母(包括非ascii字母),请使用p{L}代替[a-zA-Z] See Unicode categories . 请参阅Unicode类别

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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