简体   繁体   中英

Java regex for word with dot

I need to a regex to validate a string like "foo.com" . A word which contains a dot. I have tried several but could not get it work.

The patterns I have tried:

  1. (\\\\w+\\\\.)
  2. (\\\\w+.)
  3. (\\\\w.)
  4. (\\\\W+\\\\.)

Can some one please help me one this.

Thanks,

Use regex with character class

([\\w.]+)

If you just want to contain single . then use

(\\w+\\.\\w+)

In case you want multiple . which is not adjacent then use

(\\w+(?:\\.\\w+)+)

I understand your question like, you need a regex to match a word which has a single dot in-between the word (not first or last).

Then below regex will satisfy your need.

^\\w+\\.\\w+$

This regex works:
[\\w\\[.\\]\\\\]+

Tested for following combinations:
foo.com
foo.co.in
foo...
..foo

To validate a string that contains exactly one dot and at least two letters around use match for

\w+\.\w+

which in Java is denoted as

\\w+\\.\\w+

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