In regular expressions, only uppercase Roman letters and half-width spaces should be valid values.

Asked 2 years ago, Updated 2 years ago, 67 views

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

regular-expression

2022-09-30 19:24

1 Answers

\s means blank and includes TAB, CR, and LF.
To sum up the questions,

  • From top to bottom, match A to Z characters or spaces
  • From beginning to end matches non-line feed characters

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.


2022-09-30 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.