Html5 input pattern

2019-09-11 07:16发布

I have this pattern .*?[a-z]([a-z])([a-z])([a-z]).*?(\s+)(\d+) from some online generator but it's not working.

I need pattern which allow:

"minimum 3 char length string, next single space, and next minimum 1 char integer" [abc 1] or the same but in different order "minimum 1 char integer, next single space, and next minimum 3 char length string" [3 gtf].

Thanks.

2条回答
冷血范
2楼-- · 2019-09-11 07:37

The following expression should do what you describe:

[a-z]{3,}\s\d+|\d+\s[a-z]{3,}

Some notes:

  1. The {3,} construct allows you to specify repetitions within a given range. In general, it's {min,max}, but you can leave out either min or (as here) max to have an open-ended range. You can also specify an exact number of repeats by specifying a single number, e.g. {99}.

  2. The expression is essentially two complete expressions switched with alternation. Regular expressions don't allow you to say "exactly these things, in any order".

  3. I've used [a-z] to match the characters in the non-numeric part, as this is what you did in your original example.

查看更多
手持菜刀,她持情操
3楼-- · 2019-09-11 07:55

Try this one:

\d+\s\w{3,}|\w{3,}\s\d+

It either matches:

  1. One or more digits (\d+), followed by a single whitespace (\s), followed by three or more word characters (\w{3,}) (this is [a-zA-Z0-9], you can replace this part with [a-zA-Z] if you'd like).
  2. Three or more word characters (this is [a-zA-Z0-9], you can replace this part with [a-zA-Z] if you'd like), followed by a single whitespace, followed by one or more digits.

Regex101

查看更多
登录 后发表回答