简体   繁体   中英

Split using regex considering only first occurence of the regex pattern

here is my code to split a string patter based on regex match.

string[] docPath = Regex.Split("\\\\sds\\dsd\\df\\df\\d\\fd\\D\\sd\\asdsf\\sdf\\D\\dsfsd", @"[\\][A-Z][\\]");

The above code splits input string (hardcoded) into 3 parts ie

  1. \\\\sds\\dsd\\df\\df\\d\\fd
  2. sd\\asdsf\\sdf
  3. dsfsd

I want it to split only on the first occurrence. output i want is :

  1. \\\\sds\\dsd\\df\\df\\d\\fd
  2. sd\\asdsf\\sdf\\D\\dsfsd

Can anyone please help me to modify the reg expression? Kindly help.

Use the version of Regex.Split() that's an instance method which takes a number specifying the maximum number of components to split into:

Regex pattern = new Regex(@"[\\][A-Z][\\]");
string[] docPath = pattern.Split(
   "\\\\sds\\dsd\\df\\df\\d\\fd\\D\\sd\\asdsf\\sdf\\D\\dsfsd", 2);

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