字符串的正则表达式,其中某个位置包含一个或多个字母 [英] Regular Expression for a string that contains one or more letters somewhere in it

查看:75
本文介绍了字符串的正则表达式,其中某个位置包含一个或多个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正则表达式,如果字符串中的任何位置有一个或多个字母,则该表达式的计算结果为true.

What would be a regular expression that would evaluate to true if the string has one or more letters anywhere in it.

例如:

1222a3999 为真

a222aZaa 为真

aaaAaaaa 为真

但是:

1111112())-为假

我尝试过: ^ [a-zA-Z] + $ [a-zA-Z] + ,但是当有任何数字和其他字符时,它们都不起作用在字符串中.

I tried: ^[a-zA-Z]+$ and [a-zA-Z]+ but neither work when there are any numbers and other characters in the string.

推荐答案

.* [a-zA-Z].*

上面的意思是一个字母,前后都可以-一切正常.

The above means one letter, and before/after it - anything is fine.

在Java中:

String regex = ".*[a-zA-Z].*";
System.out.println("1222a3999".matches(regex));
System.out.println("a222aZaa ".matches(regex));
System.out.println("aaaAaaaa ".matches(regex));
System.out.println("1111112())-- ".matches(regex));

将提供:

true
true
true
false

符合预期

这篇关于字符串的正则表达式,其中某个位置包含一个或多个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆