正则表达式,找到x然后N个字符N + 1 == x

时间:2012-05-08 06:20:01

标签: regex string split

好的,这就是我所拥有的:

(24(?:(?!24).)*)

它的工作原理是它从24发现到下一个24但不是第二个24 ...(哇有些逻辑)。


像这样:

    23252882240013152986400000006090000000787865670000004524232528822400513152986240013152986543530000452400

它从第一个24到下一个24找到但不包括它,所以它找到的字符串是:

    23252882 - 2400131529864000000060900000007878656700000045 - 2423252882 - 2400513152986 - 24001315298654353000045 - 2400

这是我想要它做的一半,我需要它找到的是:

    23252882 - 2400131529864000000060900000007878656700000045 - 2423252882240051315298624001315298654353000045 - 2400

让我们说:

x = 24
n = 46

我需要:

find x then n characters if the n+1 character == x

所以找到接下来的46开始,第45个必须是下一个字符串的开头,包括该字符串中的所有24个字符串。

希望这很清楚。

提前致谢。

修改

   answer = 24.{44}(?=24)

2 个答案:

答案 0 :(得分:4)

你快到了。

首先,找到x(24):

24

然后,找到n = 46个字符,其中46 包含原始24(因此剩下44个):

.{44}

以下字符必须x(24):

(?=24)

所有在一起:

24.{44}(?=24)

你可以玩它here

在从给定xn构建此类正则表达式方面,您的正则表达式由

组成
x.{n-number_of_characters(x)}(?=x)

您在x中原样替换并计算n-number_of_characters(x)

答案 1 :(得分:0)

试试这个:

(?(?=24)(.{46})|(.{25})(.{24}))

<强>解释

<!--
(?(?=24)(.{46})|(.{25})(.{24}))

Options: case insensitive; ^ and $ match at line breaks

Do a test and then proceed with one of two options depending on the result of the text «(?(?=24)(.{46})|(.{25})(.{24}))»
   Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=24)»
      Match the characters “24” literally «24»
   If the test succeeded, match the regular expression below «(.{46})»
      Match the regular expression below and capture its match into backreference number 1 «(.{46})»
         Match any single character that is not a line break character «.{46}»
            Exactly 46 times «{46}»
   If the test failed, match the regular expression below if the test succeeded «(.{25})(.{24})»
      Match the regular expression below and capture its match into backreference number 2 «(.{25})»
         Match any single character that is not a line break character «.{25}»
            Exactly 25 times «{25}»
      Match the regular expression below and capture its match into backreference number 3 «(.{24})»
         Match any single character that is not a line break character «.{24}»
            Exactly 24 times «{24}»
-->