Regular expression

How to create regular expression to match List of string elements

Can you provide a bit more specific an example? It would make it easier with which to work through where you need guidance.

Suppose I have a List of SMSes like below:
List(“Dear Customer, Your recharge of Rs.30 successfully completed”,
“Dear Venkatesh, Your recharge of Rs. 50 successfully completed”,
“Dear Customer, Your recharge of Rs. 150 successfully completed”)

Need to generate Output as a regular expression which accepts three strings by comparing each string with one another.
Ex output : Dear\s\w+,\sYour\srecharge\sof\sRs.\s\d+\ssuccessfully\scompleted

So, given your example, it appears you are trying to derive the minimum Regular Expression definition String which would, when applied to all items your list, returns a matched of true for each item.

I suspect that is something you would need to originate as I haven’t seen anything even close. And it likely a non-trivial effort. Even your “Ex output” is incorrect as it wouldn’t match the first item (no space in “Rs.30”).

That said, you sound like you settled on this as a solution to a bigger problem. Given how difficult this solution is likely to be (assuming your Lists become significantly more complex), you might want to meta-up a level to see what other kinds of “simpler” approaches might become available.

2 Likes

I learned Regular Expressions from Friedl, J. E. F. (2006). Mastering Regular Expressions (Third ed.). Sebastopol, CA: O’Reilly & Associates. It is available used from Amazon.com for $22.20 USD used and the Kindle Edition for about $7 dollars more. It takes about a week, but it is well worth the effort, and you will never regret the time spent. That being said, all you will ever need to know about Java Regular Expressions is in the Pattern class of the Java Standard Edition API documentation.

I created a class, Lexerule to automate much of the messiness of RE’s so I forget how to do it from scratch, but the basic text would look something like “Rs[.]\s?(\d+)”. Then create a Pattern expression, say pattern. Then create a Matcher from pattern. Then use Matcher.find() probably in a while loop, and you are done.