简体   繁体   中英

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. And if they don't want to, they'll have the option to keep the name without spaces: myNameLength and myNoNums matches perfectly fine.

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.

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.

That's why I'm wondering:
Is there a different way than usual for Java to code spaces with boundaries?

 @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 &&[^\\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.

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.

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.

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.

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 .

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.

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 ( "\\\ " ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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