简体   繁体   中英

How to match only numbers after a letter?

I want to match only numbers after s

What I've tried

$line = "sometext s123 text";
preg_match("/s[0-9]*([0-9])/",$line,$match);

echo $match[0]

I'm getting s123 instead of 123

$line = "sometext s123 123";
preg_match("/s(\d+)/",$line,$match);

echo $match[1];

You should capture the numbers in a capture group, and then access group 1 instead of group 0 :

preg_match("/s([0-9]+)/",$line,$match);
echo $match[1]

$match[0] will give you complete match including s .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM