代码之家  ›  专栏  ›  技术社区  ›  itdoesntwork

两个字符之间的正则表达式匹配字符串

  •  0
  • itdoesntwork  · 技术社区  · 8 年前

    假设我有一个包含包含宽度和高度的文件名的字符串。。

    “en/text/org-affiliate-250x450.en.gif”

    如何使用regex只获取由“-”和“x”包含的“250”,然后由“x”和“.”包含的“450”?

    我试着遵循这个答案,但没有成功。 Regular Expression to find a string included between two characters while EXCLUDING the delimiters

    3 回复  |  直到 8 年前
        1
  •  1
  •   Paolo    8 年前

    使用“向后看”和“向前看”:

    (?<=-|x)\d+(?=x|\.)
    
    • (?<=-|x) 在后面寻找 - x .
    • \d+
    • (?=x|\.) 展望未来 或者 . .

    试试正则表达式 here .

        2
  •  1
  •   girijesh96    8 年前

    如果使用R,则可以尝试以下解决方案

    txt = "en/text/org-affiliate-250x450.en.gif"
    x <- gregexpr("[0-9]+", txt) 
    x2 <- as.numeric(unlist(regmatches(txt, x)))
    
        3
  •  1
  •   Chayim Friedman    8 年前

    -(\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);