开发者

Regex and wild characters

I'd like to use the regex

%[ _]play[ ,_]%

where % is denotes allows some arbitrary string to appear. For example, I'd want to check that this re开发者_如何学运维gex appears in some where in some string. Is this valid?


No, it is not valid. The easiest way would be to simply remove the percentage signs, because a string doesn't need to match the specified regex exclusively.

This regex:

[ _]play[ ,_]

will match this string:

ABC play,DEF


You practically made it yourself. Just need to replace % with .*

 .*[ _]play[ ,_].*

[Edit]

As Daniel Hilgarth mentioned it does not make sense to use .*, if this is just for matching.

Although, if you need to capture start and end separately then you would use expression like this:

 (.*)[ _]play[ ,_](.*)

It will capture the start and the end as numbered groups.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜