There is a line:
ООО "Парус" How to replace it with quotes like " quotes like » and « respectively?
There is a line:
ООО "Парус" How to replace it with quotes like " quotes like » and « respectively?
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); 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]}»"; }); 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
Source: https://ru.stackoverflow.com/questions/589366/
All Articles
("(\w+)")+This is too exaggerated and then will be replaced with«$2»- Alexey Shimansky