Regular Expression (Replace)

  • Thread starter Thread starter Amy L
  • Start date Start date
A

Amy L

I must be missing something here - I am thinking this should work but it
simply does not.

String rest = "/?id=3&rd=httP://www.someurl.com"

String redir_uri = "" ;
redir_uri = Regex.Replace( rest, "(https?:/{0,2}.+)$", "$1",
RegexOptions.IgnoreCase );

My expectation is that in redir_url it would contain http://www.someurl.com.
However, it ends up containing the whole entire string.

When I check the regex independently through a regex tester $1 always ends
up showing its httP://www.someurl.com

What am I missing here?

Amy.
 
your replace expression just replace what is found by itself. You should add
something like in front of the search and before the parenthesis to match
the stuff to be removed. Like:

Regex.Replace( rest, ".*?(https?:/{0,2}.+)$", "$1",
RegexOptions.IgnoreCase );
 

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

Back
Top