简体   繁体   English

使用正则表达式添加带边界的空间

[英]Adding Spaces with a boundary using regular expressions

I'm trying to let the user add spaces within the boundaries of 10 characters for their name. 我试图让用户在其名称的10个字符的边界内添加空格。 And if they don't want to, they'll have the option to keep the name without spaces: myNameLength and myNoNums matches perfectly fine. 而且,如果他们不想这样做,他们可以选择保留名称而不能使用空格: myNameLengthmyNoNums匹配得很好。

I'm uncertain how to represent a space in Regular Expressions. 我不确定如何在正则表达式中表示space
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]* . 到目前为止,我已经尝试了以下内容以及\\\\t{0,10}","[ ]{0,10}[\\\\s]* Also I know it's not \\t or \\s because Java uses Regular expressions differently. 我也知道不是\\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));  
        }

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个空格且没有其他字符。

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"); 当然,如果您确实这样做,我还建议您将正则表达式存储为仅为您的类编译一次的Pattern -将digit声明移到类顶部: private Pattern digit = Pattern.compile("\\\\d"); at the top. 在顶部。

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); 如果要使用正则表达式检查字符串是否以空格结尾Pattern.compile("\\\\s+$").matcher(string).find() ($字符代表输入的结尾); to check if a string begins with spaces, it's Pattern.compile("^\\\\s+").matcher(string).find() . 检查字符串是否以空格开头 ,它是Pattern.compile("^\\\\s+").matcher(string).find() Again, I'd split those onto separate lines in practice; 再次,在实践中,我将它们分为几行; this was for brevity's sake. 为了简洁起见。

The \\\\t expression you mentioned is used to check explicitly for the tab character, which doesn't seem to be what you want; 您提到的\\\\t表达式用于显式检查制表符,这似乎不是您想要的; \\\\s checks for any whitespace character. \\\\s检查是否有空格字符。 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. 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; 另外,由于调用s.matches(myNameLength) ,因此测试的断言在带空格的名称上将失败,该方法将检查整个字符串是否包含1到10个单词字符; a "word character" is defined as [a-zA-Z0-9_]. “文字字符”定义为[a-zA-Z0-9_]。 Any spaces in the test string, and matches() will return false . 测试字符串中的所有空格, matches()将返回false

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. 但是它确实有点尴尬,并且在“ COMMENTS模式下不起作用。 You can also use a hexadecimal escape ( "\\\\x20" ) or a Unicode escape ( "\\\ " ). 您还可以使用十六进制转义( "\\\\x20" )或Unicode转义( "\\\ " )。

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

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