开发者

Using Regular expression to search for a value but exlude that string from the results?

开发者 https://www.devze.com 2023-04-06 14:54 出处:网络
This is prob real开发者_JS百科ly simple, I\'m new to regular expressions but say I wanted to find the 2 numbers preceding some characters. i.e \"12 qty\"

This is prob real开发者_JS百科ly simple, I'm new to regular expressions but say I wanted to find the 2 numbers preceding some characters. i.e "12 qty"

So I'm using \d\d.qty to bring back the match "12 qty" but I want to exclude the word qty?

I have tried using \d\d.qty*([^qty]*) but it doesn't work.


You need to use a positive look ahead, depends on which language of course:

(\d\d)(?=\sqty)


You could use (\d\d)(.qty) so you get back

Array
(
    [0] => Array
        (
            [0] => 12 qty
        )

    [1] => Array
        (
            [0] => 12
        )

    [2] => Array
        (
            [0] =>  qty
        )

)

Now use second item of the array and you have, what you want

0

精彩评论

暂无评论...
验证码 换一张
取 消