Help with Regex

  • Thread starter The Cleaning Wonder Boy
  • Start date
T

The Cleaning Wonder Boy

Could someone please explain to me what the (?<Key> and (?<Value>
are in the following Regex expression?

This gets relative links in an HTML string (file).


"(?<Key>href=[""'](?!#|http|ftp|mailto|javascript)(?<Value>[^""']*)
[""'])"

Here is usage:

reg = New Regex( _
"(?<Key>href=[""'](?!#|http|ftp|mailto|javascript)(?<Value>
[^""']*)[""'])", _
RegexOptions.IgnoreCase _
Or RegexOptions.Multiline _
Or RegexOptions.Compiled)

What would that be as a standard Regex expression, for Perl or any other
regex engine?
 
T

The Cleaning Wonder Boy

mrclean,
They are named groups, they would (should) be the same in Perl & other regex
engines.

A tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

Nothing on this site about it..

Not much help with no examples...

Both of the above sites provide information on named groups.

Hope this helps

Not really as helpful as I would like...
 
J

Jay B. Harlow [MVP - Outlook]

mrclean,
Nothing on this site about it..

You missed the first item on this page:

http://www.regular-expressions.info/refext.html
Not much help with no examples...
Do you want samples of the regex or samples of how to access them from your
code. The msdn page gives examples of the RegEx itself. Your question only
asked what they were, not how to use them. I will look later for some
samples, in the mean time, check out the classes in the
System.Text.RegularExpressions namespace, I would start with Groups, as the
tags you were asking about are named groups...
Not really as helpful as I would like...
How does that saying go: If you give a man a fish, he eats for a day, if you
teach a man to fish he eats for life...

Hope this helps
Jay
 
T

The Cleaning Wonder Boy

mrclean,

You missed the first item on this page:

http://www.regular-expressions.info/refext.html
It wasn't very clear what that page is to someone who doesn't know what
groupings are and the expression I posted had groupings.

How does that saying go: If you give a man a fish, he eats for a day, if you
teach a man to fish he eats for life...

Well knowing nothing about regexes, it would have been better to explain
the expression instead. I have no idea how the expression works on my
text. It is supposed to get relative hrefs from HTML. Using the same
expression in a grep tool that I have that supports regex reveals no
results. Must be that it is a .Net expression (from some VB.Net code).

What would this be for a PCRE cpable tool?


From what I read on the aforementioned site, .Net Regex grouping is NOT
the same as in Perl or Python.
 
M

Mr. Clean

mrclean,
Here is an series of articles on how to use the
System.Text.RegularExpressions.Regex class in your code.
<SNIP>

I ALREADY have VB.net code so I don't need to know how
to use it in my class, I'm trying to conmvert it to a PCRE
expression since .Net regex is NOT the same.
 
D

David

<SNIP>

I ALREADY have VB.net code so I don't need to know how
to use it in my class, I'm trying to conmvert it to a PCRE
expression since .Net regex is NOT the same.

The ?<key> stuff represents named groups. They're a .NET extension, and
AFAIK not widely supported elsewhere.

Just drop the ?<key> syntax from your regex, and use the standard
positional parameters instead.

For perl:

my $re = q((href=["'](?!#|http|ftp|mailto|javascript)([^"']*)["']));

while(<>) {

if (/$re/) {
my $key = $1;
my $value = $2;
print "match: $key -> $value\n";
} else {
print "no match\n";
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top