[英]Java RegEx with variable character range
I need to check if all characters of a string are in a specific character range. 我需要检查字符串的所有字符是否都在特定字符范围内。
I know I can check with the regular expression: 我知道我可以检查正则表达式:
input.matches("[a-z]+");
if a string only contains letters from a
to z
. 如果字符串仅包含从
a
到z
字母。 But how can I do it if the character range is defined by an given interface? 但是,如果字符范围是由给定的接口定义的,我该怎么办呢? For example, the interface
xxx
has the attribute 例如,接口
xxx
具有属性
xxx.FIRST_CHAR = 'a';
and 和
xxx.LAST_CHAR = 'z';
but the attributes can be changed. 但是属性可以更改。
Is there any way to use variable characters in regular expressions? 有什么方法可以在正则表达式中使用变量字符?
您可以使用+
运算符来连接与连接字符串相同的正则表达式模式。
str.matches(xxx.FIRST_CHAR + "[a-z]*" + xxx.LAST_CHAR);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.