2012-05-08 24 views
1

确定这里就是我:REG-EX,求x则N字符,如果N + 1 == X

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

其从24直到找到下一个24而不是第二个事实作品24 ...(哇,一些逻辑)。


这样的:

23252882240013152986400000006090000000787865670000004524232528822400513152986240013152986543530000452400 

它从第1个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) 
+0

哪些方言的正则表达式您使用的是?那你想要什么,如果算上46个字符的情况发生,下一个是不是'24'? –

+0

如果下一个是不是24什么也不做,至于什么正则表达式的.NET一个[链接](http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx) – Ja77aman

回答

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)

+0

薄荷!感谢队友效果很好, – Ja77aman

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}» 
--> 
相关问题