Heisenberg
Heisenberg

Reputation: 1508

How to extract multiple string values from a sentence using regex?

I have multiple lines of text in a following format .

Hello {user}, you have been awarded {x} points for being active for more than {y} days.

How do I extract these specific variables using Regex?

e.g. -

Hello john, you have been awarded 10 points for being active for more than 2 days.

Upon extracting, I should get john (user), 10 (x) and 2 (y) from above line.

p.s. - I am using java and I need to get it done using regex.

Upvotes: 1

Views: 817

Answers (1)

José Castro
José Castro

Reputation: 671

You could go with the following regex:

/Hello (\w+), you have been awarded (\d+) points? for being active for more than (\d+) days?\./

Do note that I included questions marks after the plural form of "points" and "days", just in case they happen to be singular.

I am also assuming that the username is comprised of word characters only; if not, the \w+ should be updated accordingly.

Upvotes: 2

Related Questions