1

I'm trying to rewrite my regex for using logical condition AND:

(?<="pies osa.")|(\w+\s?\d{1,2}) - now it is OR (Pipe)

I want it to work as find string and check regex.

I've tried: (?=.*"pies osa.")(?=\w+\s?\d{1,2}) - didn't work for me

Text which I'm using:

pies osa. Pszczol 10, miod mis kon, pies osa. Kon 15

I'm trying to print all words that have been matched. Code need to read out all words after "pies osa." with given regex, which would be only two match: - Pszczol 10 and Kon 15.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user2061352
  • 29
  • 1
  • 8

1 Answers1

0

You may use

"\\bpies\\s+osa\\W*(\\w+\\s*\\d+)"

See the regex demo. The results are in Group 1.

NOTE: If the digits may be missing, use (\\w+(?:\\s*\\d+)?) instead of (\\w+\\s*\\d+).

Details:

  • \b - word boundary
  • pies\s+osa - pies, 1+ whitespaces, osa
  • \W* - zero or more non-word chars
  • (\w+\s*\d+) - Group 1 capturing a sequence of:
    • \w+ - 1+ word chars
    • \s* - 0+ whitespaces
    • \d+ - 1+ digits

Java code:

String str = "pies osa. Pszczol 10, miod mis kon, pies osa. Kon 15";
Pattern ptrn = Pattern.compile("\\bpies\\s+osa\\W*(\\w+\\s*\\d+)");
Matcher matcher = ptrn.matcher(str);
while (matcher.find()) {
    System.out.println(matcher.group(1));
}
// => Pszczol 10, Kon 15
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see [How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow)). – Wiktor Stribiżew Dec 17 '16 at 14:59