Regex Reminder: Match the End of a String
Devin Olson April 9 2013 02:35:11 PM
Regular Expression for matching the end of a string (usually in Java)
I am posting this here because I'm tired of having to think this through every time I need it. Perhaps it will be of some use to somebody else.
^.*substring$
When searching any string using a RegEx Matches Pattern construct, the above pattern breaks down as follows:
- ^ match the beginning of the string
- . match any single character
- * match the preceding match character (in this case, any character), zero or more times.
- substring this is the substring you want to match. Make sure all control characters are escaped.
- $ match the preceding match instructions against the end of the string.
Also, if you want to perform a CASE-INSENSITIVE match (without using a Java Pattern Object),
you can add the (?i) control flag to the beginning of your regular expression:
(?i)^.*substring$
Hope this helps!
-Devin.
- Comments [0]