There is a line:

ООО "Парус" 

How to replace it with quotes like " quotes like » and « respectively?

  • for example using a regular expression - Grundy
  • And what about the order? - Rajab
  • What order? - Grundy
  • 3
    @Radzhab what a difference of order if we find occurrences with a regular schedule and replace them with symbols ... that is, roughly speaking, the regular schedule will be of the type ("(\w+)")+ This is too exaggerated and then will be replaced with «$2» - Alexey Shimansky

3 answers 3

In theory, you need to replace the quotes that are before the words on the opening, and after the words - on the closing. Something like this (perhaps you can simplify the code):

 var s = "ООО \"Парус\" и ООО \"НПП \"Рога и копыта\""; var s2 = Regex.Replace(s, "\"(\\w+)", "«$1"); var s3 = Regex.Replace(s2, "(\\w+)\"", "$1»"); Console.WriteLine(s3); 
  • And what about the case of LLC "Scientific-Production Enterprise" GARANT-SERVICE-UNIVERSITY " - Radzhab
  • @Radzhab, but what is the difference between Horns and Hoofs NPP? It will work. - Vlad
  • Amazing !!!!!! - Rajab
  • one
    @Grundy, so you can not write. Quotes of a single picture are not repeated next to each other. In addition, this expression works in greenhouse conditions (with spaces between words, without punctuation marks in quotes, etc.). Everything else needs to be refined when new conditions appear. - Vlad
  • one
    @ixSci, the source line is written according to the rules of the Russian language. It is recommended to use quotes of different patterns, and if there is no technical possibility to do this, then the closing quotation mark is put only once. Those. such a spelling is permissible (probably there was no technical possibility to use a different method), and therefore it does not contradict the rules of the Russian language. - Vlad

For the solution, you can use Regex.Replace

To get the desired part of the string to be replaced, you can use the following regular expression

 "(.+?)" 

In this case, the text between the quotes will be saved in the first group.

When you simply replace quotes, you can use an overload with a string:

 var result = Regex.Replace(source, "\"(.+?)\"", "«$1»") 

If you need any additional operations, you can use the MatchEvaluator host overload:

 var result = Regex.Replace(source, "\"(.+?)\"", m => { return $"«{m.Groups[1]}»"; }); 
  • one
    And what about if the name is GARANT-SERVICE-UNIVERSITY Research and Production Enterprise LLC - Radzhab
  • @Radzhab, look for another regular expression - Grundy
  • one
    @Radzhab, If you can have such options, then you have big problems. - ixSci
  • @Radzhab, only one multiple nesting can be? or not limited? and at the same level can there be several groups with quotes? - Grundy
  • Nesting can be in two elements, but it would be good to understand how to do for the infinite too - Rajab

Here is another option:

 string res = Regex.Replace("ООО \"Парус\" и ОАО \"Рога и копыта\"", "\"(\\w[\\w ]*\\w)\"", @"«$1»"); MessageBox.Show(res, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); 

This is a way with more stringent search terms, try several options:

  • "\"(\\w[\\w ]*\\w)\"" - company names can include letters and numbers (character class "\ w"), and consist of several words (the construct "[\\w ]*" )
  • "\"(\\a[\\a ]*\\a)\"" - this pattern is similar to the previous one, but unlike the first one, it does not allow the numbers in the names of firms ( "\\a" ).
  • "\"(\\p{IsCyrillic}[\\p{IsCyrillic} ]*\\p{IsCyrillic})\"" - "\"(\\p{IsCyrillic}[\\p{IsCyrillic} ]*\\p{IsCyrillic})\"" names can only be in Cyrillic ( "\\p{IsCyrillic}" ).

You can also try using this class of characters "\\p{Lu}" it specifies a set of capital letters.

However, sometimes situations arise that require a non-trivial approach, for example, in a similar situation:

 ООО "Научно-производственное предприятие "ГАРАНТ-СЕРВИС-УНИВЕРСИТЕТ" 

Regular expressions "out of the box" are practically not applicable, there is no unambiguous solution for such situations. Sometimes the solution can be found, for example, you can limit the size of the substring with the company name using the quantifier {n, m}, it means that the previous element must be repeated at least n times, but not more than m times. And add more accurate signs indicating the beginning of the desired fragment, in this case it is a finite set of words indicating the type of law. persons ("LLC", "JSC", "ZAO", etc.).

And nested quotes inside the name can be distinguished by a simple pattern "\s«\w" . Here is an example of such a code:

  string src = "ООО \"Парус\" и ОАО \"Рога и копыта\" при согдействии "+ "ООО \"Научно-производственное предприятие "+ "\"ГАРАНТ-СЕРВИС-УНИВЕРСИТ бла бла бл\" "; string res = Regex.Replace(src, "(ООО/ЗАО/ОАО]\s]9", @"$1«"); res = Regex.Replace(res, "\"(\\w[\\w \\-«]*\\w)\"", @"«$1»"); MessageBox.Show(res, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); 

source: https://msdn.microsoft.com/ru-ru/library/az24scfc(v=vs.110).aspx#character_classes

  • 3
    And what about if the name is GARANT-SERVICE-UNIVERSITY Research and Production Enterprise LLC - Radzhab
  • It would be nice to replace the regulars with verbatim-string-literal to reduce escaping slashes. - αλεχολυτ
  • The dash inside the company name is solved simply, we simply add it to the set of valid characters inside the company name: - rst256
  • Oh dear Rajab, you have found the weakest point of regular expressions, in a situation where there is no double quote inside the name, regular ones alone can’t do anything - rst256