C# Regex replace the first and last occurrence of

2019-08-01 01:45发布

There is a multi-line string with unescaped double quotes that prevents the string from being parsed.

String example:

ns:common.topic.description "1942: A Love Story est un film indien r\u00e9alis\u00e9 par Vidhu Vinod Chopra sorti en salles le 15 juillet 1994. Il met en vedette Anil Kapoor, Manisha Koirala et Jackie Shroff. Le long m\u00e9trage est un succ\u00e8s moyen au box-office mais lance la carri\u00e8re de Manisha Koirala."@fr; ns:common.topic.description "1942: A Love Story to bollywoodzki dramat mi\u0142osny i film akcji zrealizowany w 1993 roku przez Vidhu Vinod Chopra, autora takich film\u00f3w jak Misja w Kaszmirze, czy Eklavya: The Royal Guard. W rolach g\u0142\u00f3wnych Anil Kapoor i Manisha Koirala', w drugoplanowych Jackie Shroff, Anupam Kher, Danny Denzongpa i Pran. Film by\u0142 hitem jako musical. Premiera jego odby\u0142a si\u0119 ju\u017c po \u015bmierci nagrodzonego potem kompozytora weterana Bollywoodu Rahul Dev Burman. To historia mi\u0142osna. Jej t\u0142em jest rok 1942, w kt\u00f3rym Gandhi has\u0142em "Quit India!" wezwa\u0142 Anglik\u00f3w do odej\u015bcia z kraju po 200-letniej okupacji. Syn s\u0142u\u017c\u0105cego Anglikom Indusa zakochuje si\u0119 w c\u00f3rce bojownika o wolno\u015b\u0107 kraju."@pl;

I've tried this code to replace the first and the last occurrence of " on each line of the string (the code is bad and doesn't work, just don't know other way to do what is needed):

var freebaseFixedRdfString = Regex.Replace(freebaseFixedRdfString, "\"", delegate(Match match) { bool first = match.Index == 1; bool last = match.NextMatch().Index == 0; if (first || last) return "\"\"\""; else return match.Value; }, RegexOptions.Compiled | RegexOptions.Multiline);

How to use regex to replace the first and the last " to be replaced with """ ?

标签: c# .net regex
2条回答
\"骚年 ilove
2楼-- · 2019-08-01 02:10

If you want to replace the quote in the initial or the last position, you do not need regex. However, in your example the quotes are not in the initial or the last position on the line of text.

Here is how you can do this:

var res = Regex.Replace(text, "(?<=^[^\"]*)\"|\"(?=[^\"]*$)", "\"\"\"");

The regex uses lookahead and lookbehind constructs to replaces a quote when

  • There are no other quotes between the beginning of line and this quote, or
  • There are no other quotes between this one and the end of the line.

Demo.

var text = "Quick \"brown fox jumps \"over\" the lazy\" dog";
var res = Regex.Replace(text, "(?<=^[^\"]*)\"|\"(?=[^\"]*$)", "\"\"\"");

converts

Quick "brown fox jumps "over" the lazy" dog

to

Quick """brown fox jumps "over" the lazy""" dog
查看更多
SAY GOODBYE
3楼-- · 2019-08-01 02:25
^"|"$

You can use this to replace first and last ".See demo.

http://regex101.com/r/yP3iB0/7

查看更多
登录 后发表回答