Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Regular Expression for URL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JJ - 01 Jun 2007 15:21 GMT
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
Alexey Smirnov - 01 Jun 2007 20:03 GMT
> 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.:
[quoted text clipped - 10 lines]
>
> CurrentUrl = tagMatches[i].Result("${url}");

What about simple replace?

CurrentUrl = tagMatches[i].Result("${url}");
myString = myString.replace(CurrentUrl, newUrl);
JJ - 04 Jun 2007 18:16 GMT
> 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
Alexey Smirnov - 04 Jun 2007 20:45 GMT
> > What about simple replace?
>
[quoted text clipped - 13 lines]
>
> 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";
}
JJ - 05 Jun 2007 18:01 GMT
I didn't think of that. Thanks,

JJ

>> > What about simple replace?
>>
[quoted text clipped - 42 lines]
> return "something";
> }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.