1

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.

5
  • Are you bound to using regex? I can do it w/o it and get the data you want from it. Commented Jul 14, 2016 at 14:39
  • Hello ([^,]+) - The first group contains the name. Do the same for others. If you have control on creating the string, I wouldn't recommend using regex. Commented Jul 14, 2016 at 14:39
  • 1
    Hello (\\w+), you have been awarded (\\d+) points for being active for more than (\\d+) days should do Commented Jul 14, 2016 at 14:39
  • @MarounMaroun : Well, I have many of such string formats and many strings. So I find regex is the only generalized approach to extract groups from these. Otherwise my logic would change per every format. Commented Jul 14, 2016 at 14:46
  • "I have many such string formats" - what does this mean? Give an example of one of the "other" formats. Commented Jul 14, 2016 at 14:53

1 Answer 1

2

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.

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

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.