Regex для добавления пробела в строку в asp.net - PullRequest
0 голосов
/ 12 декабря 2018

Строка, которую мы получаем в нашем документе:

18.1Commitment fee
(a)The Parent shall pay to the Agent a fee in the Base Currency computed at the rate of:
(i)35 per cent. of the Margin per annum on that Commitment under Facility A for the Availability Period applicable to Facility A;
(ii)40 per cent. of the Margin per annum on that Commitment under Facility B for the Availability Period applicable to Facility B;

Ни у одного из них нет пробела (например ...) - Ожидаемый результат ниже:

18.1 Commitment fee
(a) The Parent shall pay......
(i) 35 per cent of the margin....
(ii) 40 per cent of the margin....

Как добавить сортировкув случае если число, то добавьте пробел ... если (а), то добавьте пробел, если такие цифры, как (i), добавьте пробел

Ниже - Regex.Replace (s, @ "^ (\ d + (?:.\ d {1,2})?) (?! [\ d \ s]) (. *) "," $ 1 $ 2 ") работает над числом - предоставлено Wiktor Stribiżew

1 Ответ

0 голосов
/ 12 декабря 2018

Попробуйте:

Regex.Replace(s, @"^(\([ivxcdlm]+\)|\([a-z]+\)|\d+\.?\d*)(.*)", "$1 $2", RegexOptions.IgnoreCase)

Regex детали:

"^"                    Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed)
"("                    Match the regex below and capture its match into backreference number 1
                       Match this alternative (attempting the next alternative only if this one fails)
      "\("             Match the character “(” literally
      "[ivxcdlm]"      Match a single character from the list “ivxcdlm” (case insensitive)
         "+"           Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      "\)"             Match the character “)” literally
   "|" 
                       Or match this alternative (attempting the next alternative only if this one fails)
      "\("             Match the character “(” literally
      "[a-z]"          Match a single character in the range between “a” and “z” (case insensitive)
         "+"           Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      "\)"             Match the character “)” literally
   "|" 
                       Or match this alternative (the entire group fails if this one fails to match)
      "\d"             Match a single character that is a “digit” (any decimal number in any Unicode script)
         "+"           Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      "\."             Match the character “.” literally
         "?"           Between zero and one times, as many times as possible, giving back as needed (greedy)
      "\d"             Match a single character that is a “digit” (any decimal number in any Unicode script)
         "*"           Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
")" 
"("                    Match the regex below and capture its match into backreference number 2
   "."                 Match any single character that is NOT a line break character (line feed)
      "*"              Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
")"  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...