简体   繁体   中英

PHP Regex pattern to get a specify word

i have some strings result of var_dump like this :

string(5) "tes37"
string(6) "SRV410"
string(6) "SRV400"
string(6) "SRV311"
string(6) "SRV302"
string(6) "SRV301"
string(6) "SRV300"
string(6) "SRV001"
string(7) "SRV_123"
string(5) "sad34"
string(7) "S 0001J"
string(8) "S 00004J"
string(8) "S 00003J"
string(8) "S 00002J"
string(6) "asdasd"
string(4) "4356"
string(3) "234"

here i want to get string with regex such as : S 0001J, S 00002J, S 00003J and S00004J

anyone can help me out to make the pattern regex ? Thank you

If you don't care about the number of zeros in the string, you can use the following regex:

S 0+[1-9]J

Change the + to a quantifier of the form {number, number} if you want to keep the number of zeros within a specific number. For example, if you want between three to five zeros, you can do S 0{3,5}[1-9]J .

Of course, this is assuming the string is following the format S (a number of zeros)(a digit)J

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