1

ファイルのすべての行をループしようとしており、「」を含む行ごとに、一致を行自体に置き換えようとしていますが、最後にも「」があります。

.NET/C# から次のような構文を使用しています。

Regex re = new Regex("/\"/"); // without escaping would be /"/
re.Replace(" someAttr=\"some text here", "$0\"");
4

3 に答える 3

2

Try the following:

Regex re = new Regex("\".*");
re.Replace(" someAttr=\"some text here", "$&\"");

First, you need to lose the slashes surrounding your regex.

According to this .NET regex reference page, $& is the reference to the entire match, not $0.

Also with your current method, you would just be replacing one double-quote with two consecutive double-quotes. Since you want to add the new double-quote to the end of the line you need to make your regex match to the end of the line, which is what the .* does.

Example: http://ideone.com/K5A7D

于 2012-08-09T16:12:23.453 に答える
0
line="some text.d.sd..dsd.";    
Regex r=new Regex("\".*");
r.Replace(line,"$0\"");
于 2012-08-09T16:36:43.017 に答える
-1

これもうまくいくはずです:

string line = "This is a string";

Regex re = new Regex("[\"].*");
re.Replace(line,"$0\"");
于 2012-08-09T16:22:31.200 に答える