regex: required character in brackets

后端 未结 3 829
北荒
北荒 2021-01-14 16:43

How do I search for a string that contains only chars from a set including x, and require it to contain x? e.g. [a-z]+ but not matching if it doesn

3条回答
  •  太阳男子
    2021-01-14 17:14

    Assuming you want to match a string of only x,y or z, match start (^) followed by zero or more y or z ([yz]*) followed by an x followed by zero or more x, y or z ([xyz]*) followed by end ($)

    ^[yz]*x[xyz]*$
    

    If you are trying to match [a-z] but with an x in there somewhere, then this should do it

    ^[a-z]*x[a-z]*$
    

提交回复
热议问题