Reputation: 23
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}.*$'
Upvotes: 2
Views: 206
Reputation: 1110
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} ]*\*?$
Upvotes: 1
Reputation: 785316
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$
: EndJava Code:
final String regex = "^(?:\\*?[\\p{L}\\h]+\\*?|\\*)$";
String s = "* cit *";
System.out.println( s.matches(regex) );
Upvotes: 1
Reputation: 626950
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}*)*+\\*?");
Upvotes: 3