I need to write a regex that validates the input for a city. Except for characters only spaces are allowed and * acts as a wildcard character, so it can be either in the end or in the beginning of the string. * character should be allowed to be the only input as well.
Accepted strings:
*city
*cit *
'*'
(only asterisk)*city one
Not accepted strings:
**
*!@
%^&*
I have written this, but it doesn't allow only '*"
as input. Any ideas?
'^.*[A-Za-zÀ-ÖØ-öø-ž ]{1,34}.*$'
CodePudding user response:
You can use
^(?:\*|\*?\p{L} (?:\h \p{L}*)* \*?)$
See the regex demo.
Details:
^
- start of string(?:\*|\*?\p{L} (?:\h \p{L}*)* \*?)
- either of the two patterns:\*
- an asterisk|
- or\*?
- an optional asterisk\p{L}
- one or more letters(?:\h \p{L}*)*
- one or more repetitions of one or more horizontal whitespaces followed with zero or more letters\*?
- an optional asterisk
$
- end of string.
In Java:
bool matched = text.matches("\\*|\\*?\\p{L} (?:\\h \\p{L}*)* \\*?");
CodePudding user response:
Here is a regex that might be shorter and efficient:
^(?:\*?[\p{L}\h] \*?|\*)$
RegEx Breakup:
^
: Start(?:
: Start non-capture group\*?
: Match an optional*
[\p{L}\h]
: Match 1 of any unicode letter or horizontal whitespace\*?
: Match an optional*
|
: OR\*
: Match a single*
)
: End non-capture group$
: End
Java Code:
final String regex = "^(?:\\*?[\\p{L}\\h] \\*?|\\*)$";
String s = "* cit *";
System.out.println( s.matches(regex) );
CodePudding user response:
Welcome to StackOverflow!
Escape the asterisk with a backslash so that it isn't interpreted as a greedy repetition quantifier: \*
.
Use beginning (^
) and end ($
) anchors to ensure that your asterisk can only occur at the start or end of the string.
Lastly, use a negative lookahead ((?!...)
) to ensure that the match can't only be two asterisks.
Putting it all together, you get:
(?!^\*\*$)^\*?[\p{L} ]*\*?$