88799

Combine a positive lookahead and negative lookahead?

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.

<strong>Here are the ones that work:</strong>

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

<strong>It should also match the following, but does not:</strong>

<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)

Answer1:

It is probably much easier to pull the XML into an XDocument using its Parse method. Then you can use XPath or other means of finding that data.

As for the regex: You regex is to complex for me to comprehend, but this is how you make a certain block optional: "(thisisoptional)?".

And you cannot account for the two different orders except by including both orders manually into the regex. So if you want to be able to match "ab" and "ba" (different order), you need the following regex: "((ab)|(ba))". So everything is twice in there. You can reduce the nastyness of this by factoring out "a" and "b" into a string variable each.

Answer2:

You could try recursively traversing the XML document and scraping every attribute and text node that matches your expression for ccnum and series and appending them to List<string> ccNumList and List<string> seriesList. If ccnum and series are in the same order in the DOM tree hierarchy then ccNumList[i] == seriesList[i].

An example of doing a recursive tree traversal is here.

Answer3:

(?<=[>\"'](\\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;

Recommend

  • JQuery CreditCard validator extend
  • Google Street View container inside Shiny Application
  • Apply kurtosis to a distribution in python
  • Converting nested html to bbCode quote tags
  • Firebase database structure - need advice
  • Verbose log abbriviations meaning in SVC, scikit-learn
  • Meteor throws throwIfSelectorIsNotId exception
  • why adding a space after `(.+?)` can completely change the result
  • double precision error when converting to scientific notation
  • Multi-line JSON read using Apache PIG
  • python regex split string while keeping delimiter with value
  • How to Divide an array on c#?
  • Scala using regex with or syntax in match case statement
  • Convert two columns Pandas data frame to dictionary of list with first column as keys
  • C# List of Panels
  • How can I replace the server in Web Component Tester
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Parse a date string in a specific locale (not timezone!)
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • How to Cache Real-time Data?
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Can I check if a recipient has an automatic reply before I send an email?
  • Java: can you cast Class into a specific interface?
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Fetching methods from BroadcastReceiver to update UI
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • HTML download movie download link
  • AES padding and writing the ciphertext to a disk file
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Updating server-side rendering client-side
  • Does CUDA 5 support STL or THRUST inside the device code?
  • How to extract text from Word files using C#?
  • When should I choose bucket sort over other sorting algorithms?
  • Unanticipated behavior
  • Compare two NSDates in iPhone
  • How to disable jQuery.jplayer autoplay?
  • Proper folder structure for lots of source files
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview