开发者

Conforming a Regex that matches the following patterns?

开发者 https://www.devze.com 2022-12-11 02:29 出处:网络
I have the following patterns in a URL. John.Smith John.Smith.1 John.Al-Smith John.al-smith.1 John.Smith.Al-Caboon

I have the following patterns in a URL.

  1. John.Smith
  2. John.Smith.1
  3. John.Al-Smith
  4. John.al-smith.1
  5. John.Smith.Al-Caboon

Where the first (.) is mandatory and with at least one character before and after the first (.), the rest of the stuff (the numbers, hyphen, and the second (.)) are optional.

I created the following Regex:

^\w+.\w+-*\w*.?\d*\w*-*\w*

Though it successfully matched all the above patterns, it also matches some undesired patterns like:

  1. "login" (Without the mandatory first dot)
  2. "users/jo开发者_StackOverflow社区hn" (with an undesired / and also without the mandatory first dot)
  3. "1234" (Invalid, the pattern has to start by a character)

What am I doing wrong here?


Problems observed with your regex

  1. "." is a meta character in regex. It matches "anything". You should escape it to match the dot. Like this: \.
  2. \w is a character class which includes small letters, caps, numbers and underscore. This explains why "1234" passed.

Try this

^[a-zA-Z]\w*(\.[-\w]+){1,2}$


maybe you should escape the dots

\.


Use this expression: \w+\S*?\.\w+\S*

I read your definition as:

  • at least one character
  • mandatory dot
  • at least one character

This ran successfully using .NET RegexOptions.ECMAScript and RegexOptions.Multiline


[a-zA-Z]\w*(.[-\w]+){1,2}$ works well...Check it out at http://regexr.com?3020g


And \w means [A-Za-z0-9_]. Which is why it matches "1234".

0

精彩评论

暂无评论...
验证码 换一张
取 消