0

this is my Set of string inside richtextbox1..

/Category/5
/Category/4
/Category/19
/Category/22
/Category/26
/Category/27
/Category/24
/Category/3
/Category/1
/Category/15
http://example.org/Category/15/noneedtoadd

i want to change all the starting "/" with some url like "http://example.com/"

output:

http://example.com/Category/5
http://example.com/Category/4
http://example.com/Category/19
http://example.com/Category/22
http://example.com/Category/26
http://example.com/Category/27
http://example.com/Category/24
http://example.com/Category/3
http://example.com/Category/1
http://example.com/Category/15
http://example.org/Category/15/noneedtoadd

just asking, what is the pattern for that? :)

1
  • 3
    Why use an impact wrench when you need a screwdriver? Commented Aug 2, 2011 at 18:20

3 Answers 3

7

You don't need a regular expression here. Iterate through the items in your list and use String.Format to build the desired URL.

String.Format(@"http://example.com{0}", str);

If you want to check to see whether one of the items in that textbox is a fully-formed URL before prepending the string, then use String.StartsWith (doc).

if (!String.StartsWith("http://")) { 
    // use String.Format 
}
Sign up to request clarification or add additional context in comments.

2 Comments

what if inside the richtextbox, there's a string that does not start with / ?
So it's either /category/1 or category/1?
4

Since you're dealing with URIs, you can take advantage of the Uri Class which can resolve relative URIs:

Uri baseUri = new Uri("http://example.com/");

Uri result1 = new Uri(baseUri, "/Category/5");
// result1 == {http://example.com/Category/5}

Uri result2 = new Uri(baseUri, "http://example.org/Category/15/noneedtoadd");
// result2 == {http://example.org/Category/15/noneedtoadd}

Comments

3

The raw regex pattern is ^/ which means that it will match a slash at the beginning of the line.

Regex.Replace (text, @"^/", "http://example.com/")

2 Comments

how about if i added multiline "?m"
I don't understand what you mean by "?m". Do you mean RegexOptions.MultiLine? If so, that doesn't matter for my regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.