问题描述:

RegEx to get domain from domain\username. I tried this but it returns the entire string.

/^(.*?)\.\\/

What am I doing incorrectly? the end result should be domain

网友答案:

Regex is quite the large hammer for such a tiny nail. Just use IndexOf.

string domain = str.SubString(0, str.IndexOf('\\'));
网友答案:

If you insist on using regular expressions then what you are doing wrong is that you are are not escaping the \ (I'm also not sure why the . is there) Try

/^(.*?)\\.*$/

However for such a simple problem you would be better served just using .IndexOf to find the \ and then .Substring to return everything before it.

网友答案:

^(.*?)\\

Will give you domain\

^.[^\\]*

Will give you domain

[^x] gives you everything but x and \ has to be escaped likeso \\.

相关阅读:
Top