0

for example, i have string "john smith 12 03 1993" task is to eject john smith to another one. Now i'm trying to use regex [a-z]\s(java validate) but it not works giving just "smith" without john

String test = "john smith 11 12323 2323";
    Pattern pattern = Pattern.compile("[a-z]*\\s*");
    Matcher matcher = pattern.matcher(test);
    matcher.matches();
    matcher.find();
    System.out.println(matcher.group());
4
  • That's because it matches a sequence of letters followed by a space, and you want to match two sequences of letters separated by a space (and maybe followed by a space?) Commented Feb 2, 2017 at 15:01
  • Also, I'm not sure it's relevant to use regex here, you would have sooner accomplished your goal by using String.split(" ") Commented Feb 2, 2017 at 15:02
  • Remember that "john " and "smith " both match your pattern. You may want to print all returned groups to see what's getting matched. If the pattern always starts the line, you could try "^[a-z]*\\s*[a-z]* " Commented Feb 2, 2017 at 15:05
  • Also, if you're matching on space explicitly use a space (" ") instead of \s. Commented Feb 2, 2017 at 15:21

3 Answers 3

1

You only match 1 chunk of 0+ letters and 0+ whitespaces.

You need to use matcher.find() in a while loop and use a regex like [a-z]+ or \b[a-z]+\b:

String test = "john smith 11 12323 2323";
Pattern pattern = Pattern.compile("[a-z]+");
Matcher matcher = pattern.matcher(test);
while (matcher.find()) {
    System.out.println(matcher.group());
}
// => john, smith

See the Java demo.

If you need to match "words" before whitespace/end of string, use "\\b[a-z]+(?!\\S)" regex.

To match john smith you may use "^[a-z]+(?:\\s+[a-z]+)*" regex (and then you may replace while with if since you only expect 1 match).

String test = "john smith 11 12323 2323";
Pattern pattern = Pattern.compile("^[a-z]+(?:\\s+[a-z]+)*");
Matcher matcher = pattern.matcher(test);
if (matcher.find()) {
    System.out.println(matcher.group());
} // => john smith

See this Java demo.

Sign up to request clarification or add additional context in comments.

Comments

1
    String test = "john smith 11 12323 2323";
    Pattern pattern = Pattern.compile("^([a-z]+ [a-z]+).*");
    Matcher matcher = pattern.matcher(test);
    matcher.matches();
    System.out.println(matcher.group(1));

group(0) is always the entire String (test). Replace ^ with .* if the word is not the first thing in "test"

1 Comment

Note that this matches space " " explicitly. Use \s or \s+ if required.
1

Java has some tools to make this easier: named groups in particular allow you to write readable regex and extract fields with ease:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Class {
  public static void main(String... args) {
    final String test = "Андрей Парфёнов 05 15 1955";
    final Pattern pattern = Pattern.compile("" +
      "^(?<name>" +
        "(?<firstName>.+)\\s+" +
        "(?<lastName>.+))\\s+" +
      "(?<date>" +
        "(?<month>\\d{2})\\s+" +
        "(?<day>\\d{2})\\s+" +
        "(?<year>\\d{4}))$");
    final Matcher matcher = pattern.matcher(test);
    matcher.matches();
    System.out.println(matcher.group("name"));
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.