简体   繁体   English

正则表达式,字母使用偶数/奇数次

[英]Regular expression, letter used even/odd-numbered times

Thread's title might be little bit misleading, but I didnt find out any better way to name it. Thread的标题可能有点误导,但是我没有找到更好的命名方式。 I want to find a way using regex to look for words that contains only letters x and y, but x has to be used an even-numbered times and y has to be used odd-numbered times. 我想找到一种使用正则表达式来查找仅包含字母x和y的单词的方法,但是x必须使用偶数次,而y必须使用奇数次。

I would know how to do that with two regex's. 我会用两个正则表达式来做到这一点。

This one checks if there is an even number of x (and at least two): 这个检查x是否存在偶数(至少两个):

/^(y*xy*x)+y*$/

And this one checks if there is an odd number of y (and at least one): 然后检查是否存在奇数个y(至少一个):

/^(x*yx*y)*x*yx*$/

Both will match only strings exclusively formed of x an y. 两者都将仅匹配仅由x和y组成的字符串。

I've tested this in my editor: 我已经在编辑器中对此进行了测试:

(?=([^x]*x[^x]*x[^x]*)*)(?=([^y]*y[^y]*y[^y]*)*[^y]*y[^y]*)

This will find substrings, so you may want to add space, beginning, end of line characters: 这将找到子字符串,因此您可能需要添加空格,行首,行尾字符:

[^\s](?=([^x]*x[^x]*x[^x]*)*)(?=([^y]*y[^y]*y[^y]*)*[^y]*y[^y]*)[$\s]

To break it down, [^x]*x[^x]*x[^x]* will grab all not-x, until it finds the first x, then grabs whatever following non-x until it finds the second x, then again grabs whatever else follows until another x. 为了解决这个问题, [^x]*x[^x]*x[^x]*将抢占所有非x的对象,直到找到第一个x,然后抢占所有非x的对象,直到找到第二个x,然后再次抓取其他所有内容,直到另一个x。 This is enclosed in ( )* so that it is repeated unlimited number of times, so that it finds all pairs of x's. 它包含在()*中,因此可以无限次重复,以便找到所有x对。 If there is an odd x left over (odd x's), the regex will not be able to match that single x left. 如果剩下一个奇数x(奇数x),则正则表达式将无法匹配该单个x。 Similarly for the y case, it matches all pairs of y's, then makes sure there is a single y left followed by all not-y's. 类似地,对于y情况,它匹配所有对y,然后确保剩下一个y,然后是所有非y。 I used ?= so that it matches but does not capture, so that we can check for x's then in the same string check for y's. 我使用?=使其匹配但不捕获,因此我们可以检查x,然后在同一字符串中检查y。

Here's a tested JavaScript example function implementing a single regex that meets your requirements. 这是一个经过测试的JavaScript示例函数,实现了一个满足您要求的正则表达式。 I am assuming that zero is an acceptable (even) number of Xs. 我假设零是可接受的X数(偶数)。 The regex is documented in a multi lined comment. 正则表达式记录在多行注释中。

function has_even_X_odd_Y_word(text) {
/*#!(?#!js\/i re_even_x_odd_y Rev:20170125_2100)
    # Match word having even count of X and odd count of Y.
    \b       # Anchor to start of word.
    # First assert odd, non-zero count of Ys in this word.
    (?=      # Assert odd count of Y.
      (?:    # Zero or more or more Y pairs.
        x*y  # Optional Xs preceeding 1st Y.
        x*y  # Optional Xs preceeding 2nd Y.
      )*     # End zero or more Y pairs.
      x*yx*  # Optional Xs around last (odd) Y.
      \b     # Anchor to end of word.
    )        # End assert odd count of Y.
    # Match even number of Xs (can be zero = even).
    (?:      # Zero or more X pairs.
      y*x    # Optional Ys preceeding 1st X.
      y*x    # Optional Ys preceeding 2nd X.
    )*       # End zero or more X pairs.
    y*       # Optional Ys after last (even) X.
    \b       # Anchor to end of word.
!#*/
    var re_even_x_odd_y = /\b(?=(?:x*yx*y)*x*yx*\b)(?:y*xy*x)*y*\b/i;
    if (re_even_x_odd_y.test(text)) { return true; }
    return false;
} // End has_even_X_odd_Y_word().

The function returns true if a matching word is found in the passed text and false otherwise. 如果在传递的文本中找到匹配的单词,则该函数返回true,否则返回false。

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

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