0

Let's say I have a string containing a filename that includes width and height.. eg.

"en/text/org-affiliate-250x450.en.gif"

how can I get only the "250" contained by '-' and 'x' and then the "450" containd by 'x' and '.' using regex?

I tried following this answer but with no luck. Regular Expression to find a string included between two characters while EXCLUDING the delimiters

itdoesntwork
  • 1,745
  • 16
  • 31

3 Answers3

3

If you are using R then you can try following solution

txt = "en/text/org-affiliate-250x450.en.gif"
x <- gregexpr("[0-9]+", txt) 
x2 <- as.numeric(unlist(regmatches(txt, x)))
girijesh96
  • 455
  • 1
  • 4
  • 16
1

Use a lookbehind and a lookahead:

(?<=-|x)\d+(?=x|\.)
  • (?<=-|x) Lookbehind for either a - or a x.
  • \d+ Match digits.
  • (?=x|\.) Lookahead for either a x or a ..

Try the regex here.

Paolo
  • 21,270
  • 6
  • 38
  • 69
1

Use the regex -(\d)+x(\d+)\.:

var str = 'en/text/org-affiliate-250x450.en.gif';
var numbers = /-(\d+)x(\d+)\./.exec(str);
numbers = [parseInt(numbers[1]), parseInt(numbers[2])];
console.log(numbers);
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77