I'm trying to let the user add spaces within the boundaries of 10 characters for their name. And if they don't want to, they'll have the option to keep the name without spaces: myNameLength
and myNoNums
matches perfectly fine.
我试图让用户在10个字符的边界内为其名称添加空格。如果他们不想,他们可以选择保留名称不带空格:myNameLength和myNoNums完全匹配。
I'm uncertain how to represent a space
in Regular Expressions.
I've used this website to help me understand Regular Expressions a bit more as well as JPS, but I'm unable to find the right syntax.
我不确定如何在正则表达式中表示空格。我使用这个网站来帮助我理解正则表达式以及JPS,但我无法找到正确的语法。
So far I've tried what's written bellow as well as \\t{0,10}","[ ]{0,10}
,and [\\s]*
. Also I know it's not \t
or \s
because Java uses Regular expressions differently.
到目前为止,我已经尝试了下面的内容以及\\ t {0,10}“,”[] {0,10}和[\\ s] *。另外我知道它不是\ t或\ s,因为Java使用正则表达式的方式不同。
That's why I'm wondering:
Is there a different way than usual for Java to code spaces with boundaries?
这就是为什么我想知道:Java是否有不同的方式来编写带边界的空间?
@Test
public void testName()
{
String myNameLength = "\\w{1,10}";
String myNoNums = "[^\\d]{1,10}";
String mySpaces = "\\s{0,10}";
Player p = new Player();
p.Player("Antonio");
String s = p.getName();
assertTrue(s.matches(myNameLength) && s.matches(myNoNums) && s.matches(mySpaces));
}
3 个解决方案
#1
1
Validating names with regex is not a good idea, but if you want to allow any character, spaces included, you can use the regex:
使用正则表达式验证名称不是一个好主意,但如果您想允许任何字符,包含空格,您可以使用正则表达式:
.{1,10}
But if you want to have only \w
, without digits but with space, you can use the single regex:
但是如果你想只有\ w,没有数字但有空格,你可以使用单个正则表达式:
[\\w &&[^\\d]]{1,10}
With the current setup of your code, you were making sure that the name mached 0 to 10 spaces and no other characters.
使用当前的代码设置,您确保名称匹配0到10个空格而没有其他字符。
#2
0
I'm a little fuzzy on your requirements, but from reading your question, it looks like the only thing you're trying to do is exclude digits and constrain the length, so you don't need to worry about explicitly checking for spaces; something like
我对你的要求有点模糊,但是通过阅读你的问题,看起来你要做的唯一事情是排除数字和约束长度,所以你不必担心明确检查空格;就像是
public boolean isValid(String name) {
if (name==null) {
return false;
}
int len = name.length();
Pattern digit = Pattern.compile("\\d");
return len>0 && len<=10 && !digit.matcher(name).find();
}
should work if you really want to use regexes. Of course, if you do go this route, I'd also recommend storing the regex as a Pattern
only compiled once for your class - move the digit
declaration to the top of the class: private Pattern digit = Pattern.compile("\\d");
at the top.
如果你真的想使用正则表达式应该工作。当然,如果你选择这条路线,我还建议将正则表达式存储为仅为你的类编译一次的模式 - 将数字声明移到类的顶部:private Pattern digit = Pattern.compile(“\\ d“);在顶部。
Edit: Realized I didn't answer your explicit question. I'm not sure what you mean by "spaces with boundaries". If you want to use a regex to check whether a string ends with spaces, it's Pattern.compile("\\s+$").matcher(string).find()
(the $ character stands for the end of the input); to check if a string begins with spaces, it's Pattern.compile("^\\s+").matcher(string).find()
. Again, I'd split those onto separate lines in practice; this was for brevity's sake.
编辑:已实现我没有回答您的明确问题。我不确定你的意思是“有边界的空间”。如果你想使用正则表达式来检查一个字符串是否以空格结尾,那就是Pattern.compile(“\\ s + $”)。matcher(string).find()($字符代表输入的结尾);检查一个字符串是否以空格开头,它是Pattern.compile(“^ \\ s +”)。matcher(string).find()。再一次,我在实践中将它们分成不同的部分;这是为了简洁起见。
The \\t
expression you mentioned is used to check explicitly for the tab character, which doesn't seem to be what you want; \\s
checks for any whitespace character. You can also negate these special regex characters by capitalizing them; string.matches("\\S+")
will tell you if a string consists entirely of non-whitespace characters.
您提到的\ t表达式用于显式检查制表符,这似乎不是您想要的; \\ s检查任何空格字符。你也可以通过大写它们来否定这些特殊的正则表达式字符; string.matches(“\\ S +”)将告诉您字符串是否完全由非空白字符组成。
Also, your test's assertion will fail on names with spaces because you're calling s.matches(myNameLength)
, which checks that the entire string consists of between 1 and 10 word characters; a "word character" is defined as [a-zA-Z0-9_]. Any spaces in the test string, and matches()
will return false
.
此外,您的测试断言将在带空格的名称上失败,因为您正在调用s.matches(myNameLength),它会检查整个字符串是否包含1到10个单词字符; “单词字符”定义为[a-zA-Z0-9_]。测试字符串中的任何空格和matches()都将返回false。
#3
0
Do you mean you want to allow up to ten letters, along with optional spaces that don't count toward the length? You might be looking for this:
你的意思是你想要允许最多十个字母,以及不计入长度的可选空格?你可能正在寻找这个:
"^(?:[a-z] *){1,9}[a-z]$" // spaces allowed within the name, but not at the ends
...or this:
"^(?: *[a-z]){1,10} *$" // spaces allowed everywhere
Be sure and specify CASE_INSENSITIVE
mode when you compile the regex.
编译正则表达式时,请确保并指定CASE_INSENSITIVE模式。
As for matching a space character in a regex, there's no trick; you just use a literal space, as I did. But it does look a little awkward, and it won't work in COMMENTS
mode. You can also use a hexadecimal escape ("\\x20"
) or a Unicode escape ("\\u0020"
).
至于匹配正则表达式中的空格字符,没有技巧;你就像我一样使用文字空间。但它确实看起来有点尴尬,它不会在COMMENTS模式下工作。您还可以使用十六进制转义符(“\\ x20”)或Unicode转义符(“\\ u0020”)。
#1
1
Validating names with regex is not a good idea, but if you want to allow any character, spaces included, you can use the regex:
使用正则表达式验证名称不是一个好主意,但如果您想允许任何字符,包含空格,您可以使用正则表达式:
.{1,10}
But if you want to have only \w
, without digits but with space, you can use the single regex:
但是如果你想只有\ w,没有数字但有空格,你可以使用单个正则表达式:
[\\w &&[^\\d]]{1,10}
With the current setup of your code, you were making sure that the name mached 0 to 10 spaces and no other characters.
使用当前的代码设置,您确保名称匹配0到10个空格而没有其他字符。
#2
0
I'm a little fuzzy on your requirements, but from reading your question, it looks like the only thing you're trying to do is exclude digits and constrain the length, so you don't need to worry about explicitly checking for spaces; something like
我对你的要求有点模糊,但是通过阅读你的问题,看起来你要做的唯一事情是排除数字和约束长度,所以你不必担心明确检查空格;就像是
public boolean isValid(String name) {
if (name==null) {
return false;
}
int len = name.length();
Pattern digit = Pattern.compile("\\d");
return len>0 && len<=10 && !digit.matcher(name).find();
}
should work if you really want to use regexes. Of course, if you do go this route, I'd also recommend storing the regex as a Pattern
only compiled once for your class - move the digit
declaration to the top of the class: private Pattern digit = Pattern.compile("\\d");
at the top.
如果你真的想使用正则表达式应该工作。当然,如果你选择这条路线,我还建议将正则表达式存储为仅为你的类编译一次的模式 - 将数字声明移到类的顶部:private Pattern digit = Pattern.compile(“\\ d“);在顶部。
Edit: Realized I didn't answer your explicit question. I'm not sure what you mean by "spaces with boundaries". If you want to use a regex to check whether a string ends with spaces, it's Pattern.compile("\\s+$").matcher(string).find()
(the $ character stands for the end of the input); to check if a string begins with spaces, it's Pattern.compile("^\\s+").matcher(string).find()
. Again, I'd split those onto separate lines in practice; this was for brevity's sake.
编辑:已实现我没有回答您的明确问题。我不确定你的意思是“有边界的空间”。如果你想使用正则表达式来检查一个字符串是否以空格结尾,那就是Pattern.compile(“\\ s + $”)。matcher(string).find()($字符代表输入的结尾);检查一个字符串是否以空格开头,它是Pattern.compile(“^ \\ s +”)。matcher(string).find()。再一次,我在实践中将它们分成不同的部分;这是为了简洁起见。
The \\t
expression you mentioned is used to check explicitly for the tab character, which doesn't seem to be what you want; \\s
checks for any whitespace character. You can also negate these special regex characters by capitalizing them; string.matches("\\S+")
will tell you if a string consists entirely of non-whitespace characters.
您提到的\ t表达式用于显式检查制表符,这似乎不是您想要的; \\ s检查任何空格字符。你也可以通过大写它们来否定这些特殊的正则表达式字符; string.matches(“\\ S +”)将告诉您字符串是否完全由非空白字符组成。
Also, your test's assertion will fail on names with spaces because you're calling s.matches(myNameLength)
, which checks that the entire string consists of between 1 and 10 word characters; a "word character" is defined as [a-zA-Z0-9_]. Any spaces in the test string, and matches()
will return false
.
此外,您的测试断言将在带空格的名称上失败,因为您正在调用s.matches(myNameLength),它会检查整个字符串是否包含1到10个单词字符; “单词字符”定义为[a-zA-Z0-9_]。测试字符串中的任何空格和matches()都将返回false。
#3
0
Do you mean you want to allow up to ten letters, along with optional spaces that don't count toward the length? You might be looking for this:
你的意思是你想要允许最多十个字母,以及不计入长度的可选空格?你可能正在寻找这个:
"^(?:[a-z] *){1,9}[a-z]$" // spaces allowed within the name, but not at the ends
...or this:
"^(?: *[a-z]){1,10} *$" // spaces allowed everywhere
Be sure and specify CASE_INSENSITIVE
mode when you compile the regex.
编译正则表达式时,请确保并指定CASE_INSENSITIVE模式。
As for matching a space character in a regex, there's no trick; you just use a literal space, as I did. But it does look a little awkward, and it won't work in COMMENTS
mode. You can also use a hexadecimal escape ("\\x20"
) or a Unicode escape ("\\u0020"
).
至于匹配正则表达式中的空格字符,没有技巧;你就像我一样使用文字空间。但它确实看起来有点尴尬,它不会在COMMENTS模式下工作。您还可以使用十六进制转义符(“\\ x20”)或Unicode转义符(“\\ u0020”)。