views:

730

answers:

7

How do I convert the string:

"Microsoft Windows XP Professional x64 Edition|C:\\WINDOWS|\\Device\\Harddisk4\\Partition1"

to

"Microsoft Windows XP Professional x64 Edition"

...using regular expressions?

I want to cut out all after | symbol. Is it easy to realise it via Regex.Replace? Where could I found syntax description for Regex.Replace patterns?

+13  A: 

You don't need a Regex for that. You can use substring:

var text = @"Microsoft Windows XP Professional x64 Edition|C:\WINDOWS|\Device\Harddisk4\Partition1";
text = text.Substring(0,text.IndexOf("|"));
driis
But just in case the text does not contain "|", then you will have an exception.So better put it in try catch if not always sure about having "|" in the input string.
Ganesh R.
Or even better, do it in three lines, with an if check on the result of IndexOf, and avoid the exception altogether.
Matthew Scharley
Correct, you should check the return value of IndexOf before using it, to make sure that the | character actually exists in the string. The code example illustrates the concept.
driis
+1  A: 

Use String.Split(), which yields a String[], then pick up element zero.

Andrew
@driis even better.
Andrew
+9  A: 
string str = @"Microsoft Windows XP Professional x64 Edition|C:\WINDOWS|\Device\Harddisk4\Partition1";
string str2 = str.Split('|')[0];

str2 = "Microsoft Windows XP Professional x64 Edition"

This would fail if for some reason an invalid string was entered...
James
I assume that this is not a user-entered string. If it was a) the UI wouldn't be very usable and b) the app would probably be for personal use. For a production app you would definitely have to validate the user input.
A: 

If you still want to learn some more about Regular expressions here is a good Cheat Sheet and a simple online regex builder tool to get you started.

rohancragg
A: 

A simple solution may be to use:

    string szOrig = "Microsoft Windows XP Professional x64 Edition|C:\\WINDOWS|\\Device\\Harddisk4\\Partition1";
    string[] separator = new string[] { "|" };
    string[] szTemp = szOrig.Split(separator, StringSplitOptions.RemoveEmptyEntries);
    string szRequired = szTemp[0];

May not be the best way, but works.

Ganesh R.
bwahh, should downvote that for using hungarian notation. :P
Botz3000
Agree with Botz3000 about the Hungarian. Especially since C# doesn't use zero-terminated strings, which is the sz stands for.
John M Gant
+1  A: 
string GetOSType(string data)
{
      return data.Split(Convert.ToChar("|"))[0];
}

this is assuming the string is ALWAYS going to split. Probably to be sure you would want to wrap a try - catch block around this function.

James
Couldn't you use the character constant '|' instead of calling the method Convert.ToChar("|")?
Adam Porad
@Adam, yeah you could do.
James
+1  A: 

If you're determined to use a regular expression:

Regex p = new Regex(@"([^|]*)|");
string s = @"Microsoft Windows XP Professional x64 Edition|C:\\WINDOWS|\\Device\\Harddisk4\\Partition1";
s = p.Match(s).Value;
Robert Rossney