By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,683 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,683 IT Pros & Developers. It's quick & easy.

Regular Expression for URL

P: n/a
JJ
I can get a set of matches of URL's by using a match expression that has a
named capture group in it, i.e.:

Regex reg_linkTags = new Regex("(?:.....long resular
expression......)(?<url>\\w+|\"[^\"]*\"|'[^']*')(?:(?:\\s+\\w+\\s*=\\s*)(?:
......long regular expression......)", RegexOptions.IgnoreCase |
RegexOptions.Compiled | RegexOptions.Multiline);
MatchCollection tagMatches = reg_linkTags.Matches(myString);

Then get the url named capture group by:
for (int i = 0; i <= (tagMatches.Count - 1); i++)
{

CurrentUrl = tagMatches[i].Result("${url}");
BUT how do I _replace_ the url in the original string ('myString'). I can
match them , but I cannot seem to replace them. I tried using regex.replace,
but I don't think I can use this with either 'non capturing groups' (which I
wrap the named capture group 'url' in) or with named capture groups can I?

Thanks,
JJ
Jun 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jun 1, 4:21 pm, "JJ" <a...@xyz.comwrote:
I can get a set of matches of URL's by using a match expression that has a
named capture group in it, i.e.:

Regex reg_linkTags = new Regex("(?:.....long resular
expression......)(?<url>\\w+|\"[^\"]*\"|'[^']*')(?:(?:\\s+\\w+\\s*=\\s*)(?:
.....long regular expression......)", RegexOptions.IgnoreCase |
RegexOptions.Compiled | RegexOptions.Multiline);
MatchCollection tagMatches = reg_linkTags.Matches(myString);

Then get the url named capture group by:
for (int i = 0; i <= (tagMatches.Count - 1); i++)
{

CurrentUrl = tagMatches[i].Result("${url}");
What about simple replace?

CurrentUrl = tagMatches[i].Result("${url}");
myString = myString.replace(CurrentUrl, newUrl);

Jun 1 '07 #2

P: n/a
JJ
>
What about simple replace?

CurrentUrl = tagMatches[i].Result("${url}");
myString = myString.replace(CurrentUrl, newUrl);
Ah thats what I did in the first place. However, when the href and the src
parts of the tag had identical beginning sections, both were replaced - in
my case I didn't want that to happen.

What I ended up doing was creating another reg expression to pull out just
the href url so that I could replace it. Just thought there may be any
easier way to use named capture groups to replace (not just capture) text.
Maybe there is, but as yet I've not a clue how to do it.

Thanks,

JJ
Jun 4 '07 #3

P: n/a
On Jun 4, 7:16 pm, "JJ" <a...@xyz.comwrote:
What about simple replace?
CurrentUrl = tagMatches[i].Result("${url}");
myString = myString.replace(CurrentUrl, newUrl);

Ah thats what I did in the first place. However, when the href and the src
parts of the tag had identical beginning sections, both were replaced - in
my case I didn't want that to happen.

What I ended up doing was creating another reg expression to pull out just
the href url so that I could replace it. Just thought there may be any
easier way to use named capture groups to replace (not just capture) text.
Maybe there is, but as yet I've not a clue how to do it.

Thanks,

JJ
I think there is one more possibility

Change your pattern to return all content in a groups

(?<textbefore>...)(?<url>...)(?<textafter>...)

In this case you could use the following replacement statement

string newUrl = "http://.......";
string result = reg_linkTags.Replace(text, "${textbefore}" + newUrl +
"${textafter}");

You can also use a MatchEvaluator delegate to custom string function
that can be called at each match to evaluate the replacement value.

string result = reg_linkTags.Replace(text, new
MatchEvaluator(NewUrl));

string NewUrl(Match m)
{
string x = m.Groups["Domain"].ToString();
....
return "something";
}

Jun 4 '07 #4

P: n/a
JJ
I didn't think of that. Thanks,

JJ

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
On Jun 4, 7:16 pm, "JJ" <a...@xyz.comwrote:
What about simple replace?
CurrentUrl = tagMatches[i].Result("${url}");
myString = myString.replace(CurrentUrl, newUrl);

Ah thats what I did in the first place. However, when the href and the
src
parts of the tag had identical beginning sections, both were replaced -
in
my case I didn't want that to happen.

What I ended up doing was creating another reg expression to pull out
just
the href url so that I could replace it. Just thought there may be any
easier way to use named capture groups to replace (not just capture)
text.
Maybe there is, but as yet I've not a clue how to do it.

Thanks,

JJ

I think there is one more possibility

Change your pattern to return all content in a groups

(?<textbefore>...)(?<url>...)(?<textafter>...)

In this case you could use the following replacement statement

string newUrl = "http://.......";
string result = reg_linkTags.Replace(text, "${textbefore}" + newUrl +
"${textafter}");

You can also use a MatchEvaluator delegate to custom string function
that can be called at each match to evaluate the replacement value.

string result = reg_linkTags.Replace(text, new
MatchEvaluator(NewUrl));

string NewUrl(Match m)
{
string x = m.Groups["Domain"].ToString();
...
return "something";
}

Jun 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.