结合积极的前瞻和负超前? [英] Combine a positive lookahead and negative lookahead?

查看:141
本文介绍了结合积极的前瞻和负超前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不擅长与正则表达式,但我有以下,但我相信下面的方法部分寻找13 - 16个数字,然后返回成功,如果它发现3 - 4位数之后。问题是,3 - 4个数字都是可选的,也可以是前13 - 16位的数字,所以我想我要一个正向前查找/后向,负向前查找/后向整合。这听起来方式复杂,有没有简单的方法?

 (\ d {13,16})<']。?*(= [>'] \ D {3,4}< '])[>中'](\ D {3,4})并[d']
 

这将匹配ccnum和系列在下面的代码片段:

 <信用卡式>
     NAME =李四
     ccnum =1111123412341231,
     系列=339
     EXP =03/13>
< /信用卡式>
 

不过,如果我删除ccnum或串联,它不匹配任何东西,该系列可以选配。另外该系列可以出现之前或ccnum后,如果我把一系列属性ccnum属性之前,它没有任何匹配任何内容。它也不符,如果我有一个系列ccnum为单独的元素,如或之前,如果我无视了一系列的元素:

 <信用卡式>
<系列> 234< /系列>
< ccnum> 1235583839293838< / ccnum>
< /信用卡式>
 

我所需要的正则表达式匹配下列情况下,但我不知道元素的确切名称,在这种情况下,我只是叫他们ccnum和系列。

这里是工作的:

 <信用卡式>
            < ccnum> 1235583839293838< / ccnum>
            <系列和GT 123 LT; /系列>
< /信用卡式>

<信用卡式ccnum =1838383838383833>
            <系列和GT 123 LT; /系列>
< /信用卡式>

<信用卡式ccnum =1838383838383833系列=139
< /信用卡式>
 

还应符合以下,但不会:

 <信用卡式ccnum =1838383838383833
            < /信用卡式>

<信用卡式系列=139ccnum =1838383838383833
            < /信用卡式>

<信用卡式ccnum =1838383838383833>< /信用卡式>

<信用卡式>
    <系列和GT 123 LT; /系列>
    < ccnum> 1235583839293838< / ccnum>
< /信用卡式>

<信用卡式>
< ccnum系列=123> 1235583839293838< / ccnum>
< /信用卡式>
 

现在,得到这个工作,我usinng 3个独立的定期EX pressions:

1,以匹配所收到安全code信用卡号码。

1,以配合安全code所收到信用卡号码。

1,以匹配只是一个信用卡号。

我试着结合EX pressions成或,但我结束了5组共(2从第2 EX pressions和1个来自最后一个)

解决方案

<$p$p><$c$c>(?<=[>\"'](\\d{3,4})[<\"'].{0,100})?[>\"'](\\d{13,16})[<\"'](?=.*[>\"'](\\d{3,4})[<\"'])?

这将创建三个捕捉组,其中 ccnum 总是在第二组,和系列即可在第一,第三,或没有一个组

  ccnum = match.Groups [2] .value的;
系列= match.Groups [1]。价值+ m.Groups [3] .value的;
 

I am not good with regex, but I have the following, but I assume part of the following means look for 13 - 16 digits and then return a success if it finds 3 - 4 digits after that. The problem is that the 3 - 4 digits are optional and they can also be before the 13 - 16 digit number, so I guess I want to combine a positive lookahead/lookbehind, negative lookahead/lookbehind. This sounds way to complex, is there a simpler way?

(\d{13,16})[<"'].*?(?=[>"']\d{3,4}[<"'])[>"'](\d{3,4})[<"']

which will match the ccnum and the series in the following snippet:

<CreditCard> 
     name="John Doe""
     ccnum=""1111123412341231"" 
     series="339"
     exp="03/13">
</CreditCard>

However, if I remove the ccnum or series, it doesn't match anything, and the series can be optional. Also the series can appear before or after the ccnum, so if I put the series attribute before the ccnum attribute, it doesn't match anything either. It also doesn't match if I have a series before a ccnum as separate elements, such as or if I disregard a series element:

<CreditCard> 
<series>234</series>
<ccnum>1235583839293838</ccnum>
</CreditCard>

I need the regex match the following scenarios, but I do not know the exact name of the elements, in this case, I just called them ccnum and series.

Here are the ones that work:

<CreditCard> 
            <ccnum>1235583839293838</ccnum>
            <series>123</series>
</CreditCard>

<CreditCard ccnum="1838383838383833"> 
            <series>123</series>
</CreditCard>

<CreditCard ccnum="1838383838383833" series="139"
</CreditCard>

It should also match the following, but does not:

<CreditCard ccnum="1838383838383833"
            </CreditCard>

<CreditCard series="139" ccnum="1838383838383833" 
            </CreditCard>

<CreditCard ccnum="1838383838383833"></CreditCard>

<CreditCard> 
    <series>123</series>                
    <ccnum>1235583839293838</ccnum>
</CreditCard>

<CreditCard>          
<ccnum series="123">1235583839293838</ccnum>
</CreditCard>

Right now, to get this to work, I am usinng 3 separate regular expressions:

1 to match a credit card number that comes before a security code.

1 to match a security code that comes before a credit card number.

1 to match just a credit card number.

I tried combining the expressions into an or, but I end up with 5 total groups (2 from the first 2 expressions and 1 from the last one)

解决方案

(?<=[>\"'](\\d{3,4})[<\"'].{0,100})?[>\"'](\\d{13,16})[<\"'](?=.*[>\"'](\\d{3,4})[<\"'])?

This will create three capture groups, where the ccnum is always in the second group, and the series can be in the first, the third, or none of the groups.

ccnum = match.Groups[2].Value;
series = match.Groups[1].Value + m.Groups[3].Value;

这篇关于结合积极的前瞻和负超前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆