简体   繁体   English

PHP Regex模式获取指定单词

[英]PHP Regex pattern to get a specify word

i have some strings result of var_dump like this : 我有一些类似var_dump字符串结果:

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 在这里我想用正则表达式获取字符串,例如:S 0001J,S 00002J,S 00003J和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. 如果要将零数保持在特定数字内{number, number}请将+更改为{number, number}形式的量词。 For example, if you want between three to five zeros, you can do S 0{3,5}[1-9]J . 例如,如果要在三到五个零之间,则可以执行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 当然,这是假设字符串遵循格式S (a number of zeros)(a digit)J

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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