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

fusionchart in php are not showing
php built in webserver not reacheable with ip:port via network cable
I cant get value of the radio button Except first radio button
Edit iframe content using PHP, and preg_replace()
PMPRO Wordpress plugin Membership Cancellation Errors (stripe)
Android HttpURLConnection Post Data, PHP App Server can't receive data
compare todays date with the date in the mysql table using codeigniter
Get all possible combinations without duplicates
how to upload to files to amazon EC2
Getting the top scores, but remove duplicate users (SQL)
Explode a string after 5 spaces and 2 new lines in a string in PHP
How to create a button that links to different URL using if-else statement
How to display category's items?
Distinguish tr in dynamic table form for serializing jQuery
phpMyAdmin doesn't show new databases
decode base64 and pass to zip file

Categories

HOME
qt
google-chrome
oracle
webview
keyboard
blast
postgresql-9.4
command-prompt
celery
mirc
vue-resource
iptables
bittorrent
firebase-dynamic-links
tweepy
fasm
django-rq
dma
asp.net-core-1.0
google-cloud-logging
jest
azure-machine-learning
hdl
sharedpreferences
gollum-wiki
batch-rename
environment
jpa-2.1
router
cellular-network
oracleforms
laravel-4.2
autodesk-model-derivative
infopath2010
actframework
creation
wdk
redgate
variable-variables
gitolite
sbjson
flowchart
right-click
music21
viewmodel
windows-media-player
android-mediarecorder
portability
ptvs
dynamics-crm-4
business-rules
kendo-treeview
cognos-tm1
cefpython
decoder
fuzzer
clp
livecycle
raw-sockets
bbc-micro
codecharge
swiffy
gulp-uglify
i2b2
ui4j
textpattern
miniconda
360-degrees
wlanapi
angular-amd
dbmigrate
deque
shell-extensions
mgwt
thoughtworks-go
global-scope
producer
full-text-indexing
lumia-imaging-sdk
preorder
hotswap
jchartfx
jmenubar
saga
mousemotionlistener
lru
sunstudio
rikulo
user-forums
daap
audiotoolbox
instance-variables
gamesalad
netstream
litespeed
startupscript
gaelyk
aggregator
pinax
wmd-editor
tacit-programming
jboss-mdb
activex-exe
gui-designer
rendering-engine
webclient.uploaddata

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