I want to create a pattern for the following format of string. I have come with the following format but I am stuck as I am not able to scan it properly. Below are the details
Example String: JAS 5F W 123 or BWER34 23 C 23
Above String has the following rules to be followed.
- The last digits can be 2 or 3 digit numbers only (123 && 023 or
23) - Before that only a single character is allowed case insensitive (W or c)
- Before that only 2 digits or one digit and a character only “f”or”F” is allowed.
-
Starting of string can be any String alphanumeric string of any length.
All the parts are separated by space
I came up with the following String pattern but when i run my java program it gives dangling meta character."*\\s([0-9][fF]|[1-9][0-9])\\s([a-zA-Z])\\s(\\d\\d|\\d\\d\\d)$"
Please help me in creating the correct pattern for the above String
First of all you use a quantifier but don’t quantify anything: remove the first
*or add something before it. This causes the “dangling metacharacter” message.Second
\\d\\d|\\d\\d\\dcould be rewritten to\\d{2,3}(two or three digits).Finally, you can make the expression case insensitive by adding a
(?i)prefix thus allowing you to write it as follows:Note that I assume you want to match anything before the query and thus I added a dot before the asterisk:
.*. If you usePatterndirectly (i.e. notString#matches()) you don’t even need that.Would that allow
05as well (those are two digits)? If so, you could rewrite that part as\\df|\\d{2}