How do you write a regular expression that allows you to use the special DOT character?

How do you write a regex that allows the DOT (.) Character in a username?

For instance:

R. Robert
X. A. Pauline 
+3
source share
5 answers

Delete the backslash point: \.

+5
source

[a-zA-Z. ]+ allows you to use letters, periods and spaces.

import java.util.regex.*;

public class Test {
  public static void main(String [] args) throws Exception {
    String RE = "[a-zA-Z. ]+";
    String name = args[0];
    Pattern pattern = Pattern.compile(RE);

    Matcher m = pattern.matcher(name);
    System.err.println("`" + RE + 
                       (m.matches()?"' matches `":"' does not match `") + 
                       name + "'");
  }
}

Duration:

$ java Test "R. Robert"
`[a-zA-Z., ]+' matches `R. Robert'

$ java Test "R.-Robert"
`[a-zA-Z., ]+' does not match `R.-Robert'
+3
source

, , .

\. .

, :

[A-Za-z.] . .

+2

- "" "" -, ( ) . , , , (, ..?).

, , . , - (?) .

, :

P\..R

" P", (.), , R "

P.AR
P..R
P.$R

PEAR
PA.R
P.
P\.AR

.

+2

. .

For example, if you want to get letters and periods:

[\ sh.] +

0
source

All Articles