简体   繁体   中英

Regex- Allow * in the beginning and in the end of a string and allow only *

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}.*$'

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}*)*+\\*?");

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} ]*\*?$

Here is a regex that might be shorter and efficient:

^(?:\*?[\p{L}\h]+\*?|\*)$

RegEx Demo

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) );

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