正则表达式只从字符串末尾选择数字(Regex pick only numbers from end of the string)

我需要一些关于正则表达式的帮助,例如我有这些字符串:

“Dovolenka(8)” “Bol razjedenživot26/ 26” “Parížskimhykánič.1” “Vtipnejšívyhráva1985;Deňlalae,rozhlasuatelevízi”

我需要得到数字“8”,“26”,“1”,但不是“1985”,这是一年不是零件编号。 到目前为止我得到了这个'/(\d+).?$/'用于选择数字,但我不知道如何删除preg_replace()函数的字符串,所以它只返回想要的数字。

编辑:如果我只想要字符串怎么办,反之亦然?

I need a bit of a help with regex, fro example I have these strings:

"Dovolenka (8)" "Bol raz jeden život 26/26" "Parížski mohykáni č.1" "Vtipnejší vyhráva 1985; Deň tlače,rozhlasu a televízi"

And I need to get the numbers "8", "26", "1", but not the "1985" that's a year not a part number. I got this so far '/(\d+).?$/' that works for picking the number but I do not know how to remove the string for preg_replace() function, so it only returns the wanted numbers.

Edit: What if i wanted only the strings, so vice versa ?

最满意答案

看看preg_match函数,第三个参数是匹配项的数组,当你将数字括在括号中时,你会在那里找到它们。 更确切地说,$ matches [1]包含数学数字,$ matches [0]包含整个匹配的表达式。

Have a look at preg_match function, the third parameter is the array of matched items, as you enclose your numbers in brackets, you'll find them there. To be more precise, $matches[1] contains mathcing numbers, $matches[0] contains the whole matched expression.

更多推荐