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.
精彩评论