简体   繁体   中英

RegEx how to get domain from domain\username

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 \\\\.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM