javaregexsubstring

Get substring at end of string that has no lowercase letters


I have strings like:

[POS Purchase]
POS Signature Purchase International  SKYPE COMMUNICATIO, LUXEMBOURG, LUX

or:

ATM Cash Withdrawal. Surcharge: -3.0  BNEAIR INT DP LSL4 2, BNE AIRPORT, AUS

And I want to get the end of the string that has any character but lowercase letters. For the two examples above, the answer should be:

SKYPE COMMUNICATIO, LUXEMBOURG, LUX

and

BNEAIR INT DP LSL4 2, BNE AIRPORT, AUS

How can I achieve this with a regular expression?


Solution

  • Based on your need the following regex is what you are looking for :

    [^a-z]+$
    

    The negated character class [^a-z]+ will match any combination of none lower case characters and the anchor $ will make that regex engine match the end of the string.

    But note that this will match -3.0 in your second example.And if you want to get ride of that you can put [A-Z] at the leading of your regex :

    [A-Z][^a-z]*$
    

    Regular expression visualization

    Debuggex Demo


    You can start here to learn more about regex http://www.regular-expressions.info/