I try to limit my credit card name using regular expressions, but it doesn't work.
if(source.toString().matches("^[A-Z\s]+$".toRegex())
&source.toString().matches("^[^\n]+$".toRegex())){
}
The above works, but I would like to write this in one regular expression.
※ "^[A-Z\s]+$" -> This will pass a new line
\s
means blank and includes TAB, CR, and LF.
To sum up the questions,
Therefore, as you can see in the comment section, it is easy to change the blank \s
to space .
However, there is a problem with this pattern, and you may be expecting something like FOO BAR
, but it actually matches even if the space comes first or last.On the contrary, everything matches in space.
If you want to match the name, make sure that [A-Z][A-Z][A-Z]+[A-Z]
always has [A-Z]
at the beginning and end.Also, you should make sure that the spaces are not continuous.
Also, it's hard to think that the name is one character, so I think other conditions are necessary.
^([A-Z] if you don't mind a single character.+) ([A-Z]+)*$
would be good.
© 2024 OneMinuteCode. All rights reserved.