简体   繁体   English

如何使用正则表达式从句子中提取多个字符串值?

[英]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. {user}您好,您被激活超过{y}天已获得{x}分。

How do I extract these specific variables using Regex? 如何使用Regex提取这些特定变量?

eg - 例如-

Hello john , you have been awarded 10 points for being active for more than 2 days. 约翰 ,您好,您在2天以上的活动中获得了10分。

Upon extracting, I should get john (user), 10 (x) and 2 (y) from above line. 提取后,我应该从上面的行中得到john(用户),10(x)和2(y)。

ps - I am using java and I need to get it done using regex. ps-我正在使用Java,我需要使用正则表达式来完成它。

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. 如果不是,则应相应地更新\\w+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM