php


Lookahead assertion as condition in conditional subpattern on .NET regular expression


I have this huge RegEx for matching credit cards numbers. But its PCRE. Works flawlessly in PHP.
/(\d{13,16})(?(?=<)<|["']).*?(?=(?(?=>)>|["\'])\d{3,4}(?(?=<)<|["']))(?(?=>)>|["'])(\d{3,4})(?(?=<)<|["'])/is
// /i = ignore case
// /s = treat the subject as a single line
I convert it to .NET. Just added # at the beginning and double the double quotes. I think its the proper procedure.
#"(\d{13,16})(?(?=<)<|[""]).*?(?=(?(?=>)>|[""])\d{3,4}(?(?=<)<|[""]))(?(?=>)>|[""])(\d{3,4})(?(?=<)<|[""])"
Now it doesn't match. I know PCRE and .NET implementation might not be same. But I think I can convert it to compatible one. I look up on MSDN reference. It seems my pattern has nothing special which could be PCRE specific.
After analyzing the pattern I found the (?(?=<)<|[""]) is not matching!. So made the regular expression simpler. Its now #"(?(?=q)qu|\w)\w+". And I am matching against "Queen, Quick, Qi etc"
PHP
Code
$data = "Queen, Quick, Qi etc";
$pattern = "(?(?=q)qu|\w)\w+";
preg_match_all("/$pattern/is", $data, $matches);
print_r($matches);
Output
Array
(
[0] => Array
(
[0] => Queen
[1] => Quick
[2] => etc
)
)
C# .NET
Code
string data = "Queen, Quick, Qi etc";
string pattern = #"(?(?=q)qu|\w)\w+";
Regex re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (Match m in re.Matches(data))
{
if (m.Success)
{
//Console.WriteLine("Credit Card Number={0}, CCV={1}", m.Groups[1].Value, m.Groups[6].Value);
for (int i = 1; i < m.Groups.Count; i++)
{
Console.WriteLine("[{0}][{1}]", i, m.Groups[i].Value);
for (int j = 0; j < m.Groups[i].Captures.Count; j++)
{
Console.WriteLine("[{0}][{1}][{2}]", i, m.Groups[i].Value, m.Groups[i].Captures[j].Value);
}
}
}
}
Output
Press any key to continue . . .
Output is nothing.
My questions are
Does look-ahead assertion as condition in conditional sub-pattern work on .NET regular expression?
How can I modify the simpler regular expression #"(?(?=q)qu|\w)\w+" so that it matches just like PHP in .NET?
On the first regex (the huge one) on .NET, is there anything I can apply so it matches just like PHP?
Thanks
1.: Conditionals work in .NET just as they do in PHP.
2.: The "simpler" regex is correct for .NET. You're just using it wrong:
You have no capturing groups in your regex. That means that the loop
for (int i = 1; i < m.Groups.Count; i++) {...}
is never executed because m.Groups.Count is 1.
The correct way would be something like
foreach (Match m in re.Matches(data))
{
if (m.Success)
{
for (int i = 0; i < m.Groups.Count; i++) // Groups are numbered from zero
{
// Groups[0] is the entire match
Console.WriteLine("[{0}][{1}]", i, m.Groups[i].Value);
}
}
}
3.: Your regex is missing the single quotes.
Regex regexObj = new Regex(#"(\d{13,16})(?(?=<)<|[""']).*?(?=(?(?=>)>|[""'])\d{3,4}(?(?=<)<|[""']))(?(?=>)>|[""'])(\d{3,4})(?(?=<)<|[""'])", RegexOptions.Singleline);
would be a literal translation.
4.: You don't need the /i or Ignorecase parameter as there are no letters in your regex.
5.: (?(?=<)<|["']) makes no sense. It matches exactly the same text as [<"']. After all it means "if there is a <, then match a <. Otherwise, try to match a " or a '. There is no need to use a conditional regex at all.
So the entire regex can be simplified to
(\d{13,16})[<"'].*?(?=[>"']\d{3,4}[<"'])[>"'](\d{3,4})[<"']
6.: This shows another superfluous part of the regex more clearly: You have a lookahead assertion (?=[>"']\d{3,4}[<"']) that is followed by the exact same regex [>"'](\d{3,4})[<"'], so the lookahead can be dropped entirely.
End result:
(\d{13,16})[<"'].*?[>"'](\d{3,4})[<"']
or, in C#:
Regex regexObj = new Regex(#"(\d{13,16})[<""'].*?[>""'](\d{3,4})[<""']", RegexOptions.Singleline);

Related Links

PHP - While loop for database doesn't work?
PHP Pagination Displays in Wrong Order After Certain Page
pg_connect - php script works from command line but not from web page
EL Capitan, MAMP 3.5, Apache not working, dyld: Symbol not found: _OCIInitialize
PHP Lint Mode Doesn't Catch Incompatible Declaration Errors
keeping form values after POST as a paramter of python file
Accessing $_SESSION / $_GET / … with dynamic variable
Order ASC for numbers after alphabets
Post on instagram from webpage
Convert PHP SDK to make compitable with LARAVEL 5.2
Swiftmailer-bundle with starttls support for symfony 2.8
what is wrong with the function call
Call to a member function query() on null - PHP
How do I preserve HTML from the XML in PHP?
Like & unlike voting system with PHP issue
How to turn off code coverage for phpunit 5.4+

Categories

HOME
mc
scroll
plaid
nsview
android-emulator
gallery
gentelella
elk-stack
google-search-console
cdi
phpmqtt
roku
log4j2
getopenfilename
watch-os-3
cname
sslhandshakeexception
sonata
vmware-workstation
reselect
l20n
mixpanel
cocos2d-android
nose
pfobject
wsf
adminlte
android-custom-view
android-permissions
sipp
data-collection
amazon-cloudtrail
d3.js-v4
commonsware-cwac
xcrun
universal
appcode
nppexec
wdk
xll
mars-simulator
right-click
word-cloud
fiware-wirecloud
kitura
music21
ssi
supertest
ol3-google-maps
android-sharing
word-2007
dymola
kendo-treeview
openerp-6
boost-compute
mifos
p6spy
openseadragon
oci
dojo-build
sharepoint-apps
seadragon
flask-security
grinder
gdl
largenumber
ambiguity
dnx50
apple
algebraixlib
artisan
abstract-factory
image-rotation
hotswap
boolean-algebra
profiles
lsa
frontbase
revmob
google-code-prettify
postgres-xc
tfs-sdk
significant-digits
system-requirements
clrprofiler
coordinate
image-scanner
wiktionary
expression-web
azure-acs
qcar-sdk
pinax
j2mepolish
bucket
fxruby
rtti
downcasting
sqlsitemapprovider
version-control-migration
downloadfile
service-factory

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App