开发者

C# Regular expression problem

I have the following string:

开发者_StackOverflow社区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.


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"


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.


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.


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜