C# 如何使用正则表达式匹配引号中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11859442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:32:13  来源:igfitidea点击:

How to match string in quotes using Regex

c#.netregexstring

提问by l46kok

Suppose I have the following text in a text file

假设我在文本文件中有以下文本

First Text

第一个文本

"Some Text"

“一些文字”

"124arandom txt that should not be parsed!@

"124arandom 不应解析的txt!@

"124 Some Text"

“124 一些文字”

"?? ?"

“???”

this text a"s well should not be parsed

这个文本不应该被解析

I would like to retrieve Some Text, 124 Some Textand ?? ?as matched strings. The text is read line by line. Catch is, it has to match foreign languages as well if it is inside quotes.

我想取回Some Text124 Some Text?? ?为匹配的字符串。文本是逐行阅读的。Catch 是,如果它在引号内,它也必须匹配外语。

Update: I found out something weird. I was trying some random stuff and found out that:

更新:我发现了一些奇怪的东西。我正在尝试一些随机的东西,发现:

string s = "?? ?"
Regex regex = new Regex("[^\"]*");
MatchCollection matches = regex.Matches(s);

matches have a count = 10 and have generated some empty items inside (The parsed text is in index 2). This might've been why I kept getting empty string when I was just doing Regex.Replace. Why is this happening?

匹配的计数 = 10 并在其中生成了一些空项目(解析后的文本在索引 2 中)。这可能就是我在执行 Regex.Replace 时一直得到空字符串的原因。为什么会这样?

采纳答案by Tim Pietzcker

If you read the text line by line, then the regex

如果您逐行阅读文本,则正则表达式

"[^"]*"

will find all quoted strings, unless those may contain escaped quotes like "a 2\" by 4\" board".

将查找所有带引号的字符串,除非那些可能包含转义引号,如"a 2\" by 4\" board".

To match those correctly, you need

要正确匹配这些,您需要

"(?:\.|[^"\])*"

If you don't want the quotes to become part of the match, use lookaround assertions:

如果您不希望引号成为匹配的一部分,请使用环视断言

(?<=")[^"]*(?=")
(?<=")(?:\.|[^"\])*(?=")

These regexes, as C# regexes, can be created like this:

这些正则表达式,作为 C# 正则表达式,可以像这样创建:

Regex regex1 = new Regex(@"(?<="")[^\""]*(?="")");
Regex regex2 = new Regex(@"(?<="")(?:\.|[^""\])*(?="")");

回答by YAYAYAYA

. You can use a regular expression and then try to match it with any text you want. can be in a loop or what ever you need.

. 您可以使用正则表达式,然后尝试将其与您想要的任何文本进行匹配。可以在循环中或您需要的任何内容。

string str = "\"your text\"";
//check for at least on char inside the qoutes
Regex r = new Regex("\".+\"");
bool ismatch = r.IsMatch(str);