1

I have the following string:

http://www.powerwXXe.com/text1 123-456 text2 text3/

Can someone give me advice on how to get the value of text1, text2 and text3 and put them into a string. I have heard of regular expressions but have no idea how to use them.

4
  • 1
    do you have spaces there in your url? Commented Mar 24, 2011 at 15:40
  • He never said the string was just a URL... Commented Mar 24, 2011 at 15:47
  • 5
    Do you want to have two problems? Commented Mar 24, 2011 at 15:48
  • @Ken oh the horror! +1 to snowbear then Commented Mar 24, 2011 at 19:53

6 Answers 6

5

Instead of going the RegEx route, if you know that the string will always be of a similar format, you can using string.Split, first on /, then on space and retrieve the results from the resulting string arrays.

string[] slashes = myString.Split('/');
string[] textVals = slashes[3].Split(' ');
// at this point:
//   textVals[0] = "text1"
//   textVals[1] = "123-456"
//   textVals[2] = "text2"
//   textVals[3] = "text3"
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a link on getting started with regular expressions in C#:Regular Expression Tutorial

I don't think it is appropriate to write out a tutorial here since the information is online, so please check out the link and let me know if you have a specific question.

Comments

0

Instead of using regex, you can use string.Fromat("http://myurl.com/{0}{1}{2}", value1, textbox2.Text, textbox3.Text) and format the url in whatever fashion. If you are looking to go the regex route, you can always check regexlib.

1 Comment

This one answer is the most awesome!
0

The use of regular expressions relies on patterns you see in your strings - you need to be able to generalize the pattern of strings you're looking for before you can use a regular expression.

For a problem of this scope, if you can pin down the pattern, you're probably better off using other string parsing methods, such as String.IndexOf and String.Split.

Regular expressions is a powerful tool, and certainly worth learning, but it might not be necessary here.

Comments

0

Based on the example you gave, it looks as though text1, text2 and text3 are separated by spaces? If so, and if you always know the positions they'll be in, you may want to skip regular expressions and just use .Split(' ') to split the string into an array of strings and then grab the pertinent items from there. Something like this:

string foo = "http://www.powerwXXe.com/text1 123-456 text2 text3/"
string[] fooParts = foo.Split(' ');
string text1 = fooParts[0].Replace("http://www.powerwXXe.com/", "");
string text2 = fooParts[2];
string text3 = fooParts[3].Replace("/", "");

You'd want to perform bounds checking on the string[] before trying to grab anything from it, but this would work. Regex is awesome for string parsing, but when it's simple stuff you need to do, sometimes it's overkill when simple methods from the string class will do.

Comments

0

It all depends on how much you know about about the string you are parsing. Where does the string come from and how much do you know about it's formating?

Based on your example string you could get away with something as simple as

string pattern = @"http://www.powerwXXe.com/(?<myGroup1>\S+)\s\S+\s(?<myGroup2>\S+)\s(?<myGroup3>\S+)/";
        var reg = new System.Text.RegularExpressions.Regex(pattern);

        string input = "http://www.powerwXXe.com/text1 123-456 text2 text3/";
        System.Text.RegularExpressions.Match myMatch = reg.Match(input);

The caputerd strings would then be contained in myMatch.Groups["myGroup1"], ["myGroup2"], ["myGroup3"] respectivly.

This however assumes that your string always begins with http://www.powerwXXe.com/, that there will always be three groups to capture and that the groups are separated by a space (which is an illegal character in url's and would in almost all cases be converted to %20, which would have to be accounted for in the pattern).

So, how much do you know about your string? And, as some has already stated, do you really need regular expressions?

Comments

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.