Checking if String ends with something with Java (Regex)

I need to know if we check the end of the line with something like .xyz plus any characters after it.

Like this:

myString.endsWidth(".css*") 

However, this does not pass my if-statement. One specific example of such a line: style.css?v=a1b23

Any ideas?

Full example String:

 http://xyz.com//static/css/style.css?v=e9b34 
+8
java regex
source share
5 answers

buzz, I guess something like this is even better:

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

If you really want to use regex, you can use this

 return myString.matches(".*?\\.css.*"); 
+12
source share

endsWith takes a string as parameter

matches takes a regex

+3
source share

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

+2
source share

use \.(.{3})(.*) , then the first group ($ 1) contains three characters after . and the second group of $ 2 contains the rest of the line after these three characters. Add a $ sign at the end of the expression so that it looks only for lines ending with this combination

0
source share

Try some regular expressions:

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

All Articles