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,586
  • 16
  • 30

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
  • 425
  • 1
  • 3
  • 15
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
  • 13,742
  • 6
  • 28
  • 51
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
  • 14,050
  • 2
  • 21
  • 36