wassup
wassup

Reputation: 2503

Finding a match one after another

How do I find multiple matches that are (and can only be) separated from each other by whitespaces?

I have this regular expression:

/([0-9]+)\s*([A-Za-z]+)/

And I want each of the matches (not groups) to be surrounded by a whitespace or another match. If the condition is not fullfilled, the match should not be returned.

This is valid: 1min 2hours 3days

This is not: 1min, 2hours 3days (1min and 2hours should not be returned)

Is there a simpler way of finding a continuous sequence of matches (in Java preferably) than repeating the whole regex before and after the main one, checking if there is a whitespace, start/end of the string or another match?

Upvotes: 1

Views: 1049

Answers (4)

DavidRR
DavidRR

Reputation: 19387

I believe this pattern will meet your requirements (provided that only a single space character separates your alphanumeric tokens):

(?<=^|[\w\d]\s)([\w\d]+)(?=\s|$)
    ^^^^^^^^^^  ^^^^^^^    ^^^^
        (2)       (1)       (3)
  1. A capture group that contains an alphanumeric string.
  2. A look-behind assertion: To the left of the capture group must be a) the beginning of the line or b) an alphanumeric character followed by a single space character.
  3. A look-ahead assertion: To the right of the capture group must be a) a space character or b) the end of the line.

See regex101.com demo.


Here is some sample data that I included in the demo. Each bolded alphanumeric string indicates a successful capture:

1min 2hours 3days

1min, 2hours 3days

42min 4hours 2days

Upvotes: 1

alexmac
alexmac

Reputation: 239

There is an error here the '' will match all characters and ignore your rest /([0-9]+)\s([A-Za-z]+)/

Change to

/(\d+)\s+(\w+)/g

This will return an array of matches either digits or word characters. There is no need to always write '[0-9]' or '[A-Za-z]' the same thing can be said as '\d' match any 0 to 9 more can be found at this cheat sheet regular expression cheat sheet

Upvotes: 0

wassup
wassup

Reputation: 2503

I've mananged to solve my problem by splitting the string using string.split("\\s+") and then matching the results to the pattern /([0-9]+)\s*([A-Za-z]+)/.

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

String text = "1min 2hours 3days";
boolean match = text.matches("(?:\\s*[0-9]+\\s*[A-Za-z]+\\s*)*");

This is basically looking for a pattern on your example. Then using * after the pattern its looking for zero or more occurrence of the pattern in text. And ?: means doesn't capture the group.

This will will also return true for empty string. If you don't want the empty string to be true, then change * into +

Upvotes: 1

Related Questions