0

Example:

String url = "http://www.google.com/abcd.jpg";
String url2 = "http://www.google.com/abcd.jpg_xxx.jpg";

I want to match "http://www.google.com/abcd" whatever url or url2.

I write a regex:

(http://.*\.com/[^(.jpg)]*).jpg

But [^(.jpg)]* doesn't like correct. What the regex should be?

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
Hesey
  • 4,957
  • 6
  • 31
  • 31

2 Answers2

0

Forward slash need to be escaped as well. Use this regex:

^(http:\/\/.+?\.com\/[^.]+)\.jpg

Live Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Sorry had an unnecessary `$` corrected it now. Please check now or see live demo: http://www.rubular.com/r/cxgCHLDNAr – anubhava Oct 23 '12 at 09:43
  • another question, if I want to match any substring except 'abc', what can I write? – Hesey Oct 23 '12 at 09:47
  • You can use negative lookahead for that e.g.: `(?!abc).*` Read more about lookaheads here: http://www.regular-expressions.info/lookaround.html – anubhava Oct 23 '12 at 09:50
  • or here http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word – Sergey Passichenko Oct 23 '12 at 09:52
0

Reluctant quantifier .*? matches to first ".jpg":

(http:\/\/.*\.com\/.*?)\.jpg.*
Sergey Passichenko
  • 6,920
  • 1
  • 28
  • 29