10

I need to know whether check whether a String end with something like .xyz plus any characters after it.

Like this:

myString.endsWidth(".css*")

However this does not pass my if-statement. One concrete example is this kind of string: style.css?v=a1b23

Any ideas?

Complete example String:

http://xyz.com//static/css/style.css?v=e9b34
1
  • 2
    endsWith doesn't use regular expressions. Commented Mar 30, 2012 at 13:12

5 Answers 5

15

hum, I am guessing something like this is even better:

return myString.indexOf(".css")>-1;

If you really want to go with regexp, you could use this

return myString.matches(".*?\\.css.*");
Sign up to request clarification or add additional context in comments.

1 Comment

Just tried: "style.css?v=a1b23".matches(".*?\\.css.*") and it returns 'true' so you must have another issue somewhere
3

endsWith takes a string as parameter

matches takes a regular expression

Comments

2

Use ".*\.css.+" as your regular expression.

2 Comments

I'm not sure where this does not work too? The complete string would be something like: xyz.com//static/css/style.css?v=e9b34
Did you consider Eric's answer? You need to use match(String), not endsWith(String). Updated the expression by the way.
0

use \.(.{3})(.*) then the first group ($1) contains the three characters after . and the second group $2 contains the rest of the string after those three characters. Add a $ sign at the end of the expression to make it only look for strings ending with that combination

Comments

0

Try some regex:

Pattern p = Pattern.compile(".*\.css.*");
Matcher m = p.matcher("mystring"); 
if (m.matches()) {
    // do your stuff
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.