java – Regular expressions for sampling words

Question:

Good afternoon, there is a line

select b.no as no, b.number_car as number_car from car as b where a.no = 2

How can you cut out the words after as, that is, to get no and number_car in the end. I just can't write a regular expression for this, and split doesn't help much either. I tried to discard the part from car as b where a.no = 2, it still does not work, that is, it does not look for as. Maybe someone knows?

Answer:

You need to find all occurrences of words after the whole word as , but only before the first occurrence of the word from .

Here you can use the tempered greedy token , a moderately greedy construction like (?:(?!<ДО_КАКОГО_ШАБЛОНА_ИЩЕМ>).)*? , and also start searching from the beginning of the string and only after a successful match.

String regex = "\\G(?:(?!\\bfrom\\b).)*?\\bas\\s+(\\w+)";
String string = "select b.no as no, b.number_car as number_car from car as b where a.no = 2";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println(matcher.group(1));
}
// => [no, number_car]

See Java demo

DETAILS

  • \G – start of line or end of previous match
  • (?:(?!\bfrom\b).)*? – any character (except line feed, if Pattern.DOTALL is not used), 0 or more times, as little as possible, but only if it is not the first character of the sequence from as a whole word
  • \bas\s+ – whole word as ( \b – word boundary) and 1+ whitespace characters ( \s+ )
  • (\w+) – Group 1: one or more numbers, letters, or _ .
Scroll to Top
AllEscort