Java Regex for grouping compound names

I am creating a java application that asks for some names on the console, and then with a regular expression it extracts only the names without commas and only for the correct names according to this expression:

(\\w{2,}(|\\s\\w{2,})+)

I tested the above expression on a web regex simulator and seemed to do my job fine, but when I try to match it in Java using the Pattern and Matcher classes, this doesn't work fine for names like: Alvaro de la Torre .

He continues to share the name in 4 groups (in this example). I need compound names on only one line.

I would really appreciate any help. Thanks in advance.

+4
source share
3 answers

Because we live in a deterministic world, machines follow deterministic rules. In particular, in regular expressions, if the first field of the capture group is executed, the parser will not check the second if it is not strictly necessary.

So, since the first field of your nested capture group ( (|\\s\\w{2,})) is an empty string, the regular expression satisfies this without checking another field. It is for this reason that it returns 4 separate results instead of 1.

Alternative regex:

(\\w{2,}(\\s\\w{2,}|)+)

This inverts the order of 2 fields in the capture group.

Equivalent regex:

(\\w{2,}(\\s\\w{2,})*)

Check this demo against this other one (regex is slightly different for the main reasons).

0

, "de la Torre" " " .

2 +, "de" , "" , "Torre" . , 2 Matcher, "Torre" .

, :

"(\\w{2,}(|(\\s\\w{2,})+))"

, +.

, 2 Matcher, , "de la Torre" :

Pattern pattern = Pattern.compile("(\\w{2,}(|(\\s\\w{2,})+))");
Matcher matcher = pattern.matcher("Alvaro de la Torre");
matcher.matches();
System.out.println(matcher.group(2));

" de la Torre".


:

"\\w{2,}((?:\\s\\w{2,})*)"

1, 2. , 0.


, "Alvaro de la Torre" , matcher.find() matcher.matches() matcher.group() ( 0, ). .

+2

, \w , 0-9. , Jack is 23yrs old .

-, , , , . ? *, .

-, \s , Space\x20. \t, newlines \n, \r . ,

My name is
Jack

- .

, , :

(?i)[a-z]{2,}(?:[ ][a-z]{2,})*
0
source

Source: https://habr.com/ru/post/1680061/


All Articles