How to replace "quotes with" in C# " "

There is a string:

ООО "Парус"

How do I replace the " quotes with the » and « quotes, respectively?

Author: Kromster, 2016-11-10

3 answers

In theory, you need to replace the quotation marks that stand before the words with opening ones, and after the words - with closing ones. Something like this (maybe you can simplify the code):

var s = "ООО \"Парус\" и ООО \"НПП \"Рога и копыта\"";
var s2 = Regex.Replace(s, "\"(\\w+)", "«$1");
var s3 = Regex.Replace(s2, "(\\w+)\"", "$1»");
Console.WriteLine(s3);
 6
Author: Vlad, 2016-11-10 12:33:24

For the solution, you can use Regex.Replace

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

"(.+?)"

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

If you simply replace the quotation marks, you can use an overload with a string:

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

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

var result = Regex.Replace(source, "\"(.+?)\"", m =>
{
    return $"«{m.Groups[1]}»";
});
 10
Author: Grundy, 2016-11-10 11:07:03

Here's another option:

string res = Regex.Replace("ООО \"Парус\" и ОАО \"Рога и копыта\"", 
                        "\"(\\w[\\w ]*\\w)\"", @"«$1»");

MessageBox.Show(res, "Результат", MessageBoxButtons.OK,   MessageBoxIcon.Information);

This is a method with more strict search terms, try several options:

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

You can also try using a character class like "\\p{Lu}" that specifies a lot of capital letters.

However, sometimes there are situations 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. The solution is sometimes still possible to find, 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 precise signs indicating the beginning of the desired fragment, in this case it is a finite set of words indicating the type of legal entity ("LLC", "JSC", "CJSC", etc.).

And nested quotation marks inside the name can be distinguished by a simple pattern "\s«\w". Here is an example of such 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);

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

 4
Author: rst256, 2016-11-11 07:49:14