Cozmonaut
Cozmonaut

Reputation: 3

Find Uppercase word between two characters

Trying to find words between . and =, which are uppercase (optional and contain underscore between them) like UPPER_CASE

String myString = "test.UPPER_CASE=#123456"; //Should pass

if(myString.matches(".*\\.[A-Z]=")) {
    System.out.println("Match");
} else {
    System.out.println("No Match");
}

Upvotes: 0

Views: 122

Answers (2)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

You were really close, you just forgot to match the underscore and add the quantifier:

\.[A-Z_]+=

To use it from Java you can do this:

myString.matches(".*?\\.[A-Z_]+=.*")

That's the same pattern with .*? at the beginning and .* at the end, because the matches function requires the pattern to match the whole input string. I guess there's a better API available to get a substring match, but I don't use Java myself.

Upvotes: 1

vks
vks

Reputation: 67968

.*\\.[A-Z_]+=

This should do it for you.

Upvotes: 0

Related Questions