[英]Regex: Determine if a character appears before first occurrence of another character
I'm trying to determine whether or not a certain character appears before the first (and only the first) occurrence of another character. 我正在尝试确定某个字符是否出现在另一个字符的第一个(也是第一个)出现之前。
For example, I want to determine whether the character 'X' appears before the first occurrence of 'Y'. 例如,我要确定字符“ X”是否在第一次出现“ Y”之前出现。
The regex would match these: 正则表达式将匹配以下内容:
acXaaccccYaaacacXacacaY // X appears before the first occurrence of Y
aXacacaXacacXavaY // X appears before the first occurrence of Y
The regex would NOT match this: 正则表达式与此不匹配:
acacacaYacacacXacacacY // The X appears after the first occurrence of Y
Is this possible with a single regex match? 单个正则表达式匹配是否可以实现?
You can try something like this: 你可以尝试这样的事情:
^[^Y]*X.*Y.*$
In English, that's "A string of non-Ys, then an X, then anything so long as it contains a Y". 用英语来说,就是“一串非Y,然后是X,然后是任何包含Y的东西”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.