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

发布于 2024-07-14 07:36:57 字数 128 浏览 6 评论 0原文

示例字符串

785*()&!~`a

##$%$~2343

455frt&*&*

我想捕获第一个和第三个但不是第二个,因为它不包含任何字母字符请帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼中杀气 2024-07-21 07:36:57

事实上,我认为 [a-zA-Z] 可能足以匹配您的字符串。

要捕获整个内容,请尝试:^.*[a-zA-Z].*$

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

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

攒一口袋星星 2024-07-21 07:36:57

这是一种可能的方法:

.*[a-zA-Z]+

Here is one possible way:

.*[a-zA-Z]+
如歌彻婉言 2024-07-21 07:36:57

您也许应该澄清一下“捕获”的含义:您想要仅包含 ascii 位的整个字符串吗?

另外,您没有说它是否应该只匹配普通的罗马字母(A 到 Z),或者是否还应该匹配 Unicode 字符以匹配其他语言中的字符串。

如果您只需要测试您的字符串,在 C# 中您会这样做:

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

您不需要任何其他内容,因为 myString 字符串中的任何位置只有一个字母会匹配(根据您的定义)。

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

栖迟 2024-07-21 07:36:57

这是我最喜欢的正则表达式测试网站:Javascript正则表达式测试器和备忘单

This is my favorite RegEx testing site: Javascript Regexp Tester and Cheat Sheet

小霸王臭丫头 2024-07-21 07:36:57

如果要匹配所有字母(包括非 ASCII 字母),请使用 p{L} 而不是 [a-zA-Z]。 请参阅Unicode 类别

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文