匹配每个第n个字符的正则表达式

时间:2016-05-05 09:53:41

标签: regex elasticsearch

我找到了找到第n次出现的解决方案,但找不到找到每个第n次出现的情况。

我有" key1~value1~key2~value2~key3~value3~"。

等字符串

什么是匹配~每隔一次的正则表达式?

KEY1〜VALUE1 ~ KEY2〜VALUE2 ~ KEY3〜值3 ~

我正在尝试为Elasticsearch创建一个自定义Pattern Analizer,正则表达式应该与令牌分隔符匹配而不是令牌。

3 个答案:

答案 0 :(得分:2)

您可以使用

~(?=(?:[^~]*~[^~]*~)*[^~]*$)

模式匹配:

  • ~ - 后面跟着...
  • 的代字号
  • (?=(?:[^~]*~[^~]*~)*[^~]*$) - 0+非倾斜+ ~ x 2次,0 +次,然后0+非倾斜直至字符串结尾。因此,此检查确保在匹配第一个波形符后,在字符串末尾有偶数个波浪号。

enter image description here

答案 1 :(得分:1)

您需要确保之前没有偶数(?<!^([^~]*~[^~]*~)*[^~]*)~

(?<!^([^~]*~[^~]*~)*[^~]*)~    Our regex.
                          ~    Matches a tilde (~).
(?<!                     )     Assert that before it is not:
    ^                              the beginning
     (            )*               followed by zero or more times:
      [^~]*~[^~]*~                     two tildes, no matter what comes within
                    [^~]*          followed by non-tildes.

Try it online!

工作原理:

<select name="super_attribute[141]" id="attribute141" class="required-entry super-attribute-select">
    <option value="">Choose size</option>
    <option value="36" price="0">36</option>
    <option value="38" price="0">38</option>
    <option value="41" price="0">40</option>
    <option value="43" price="0">42</option>
    <option value="45" price="0">44</option>
    <option value="47" price="0">46</option>
    <option value="49" price="0">48</option>
</select>

答案 2 :(得分:0)

~.*?(~)的第一组非重叠事件。尝试:http://regexr.com/3dc15

相关问题