简体   繁体   English

C#正则表达式第一个单词匹配

[英]c# regex first word matching

I have a string, named str, with a value of: PRINT "HELLO WORLD" 我有一个名为str的字符串,其值为: PRINT "HELLO WORLD"

I'm trying to match it using Regex in C#, but this doesn't work: 我正在尝试使用C#中的Regex来匹配它,但这不起作用:

Regex.IsMatch(str, @"^PRINT");

but if my str has a value of only PRINT , it matches. 但是,如果我的str的值只有PRINT ,那么它将匹配。

What am I going to change in my regex expression in order to match the succeeding characters enclosed in quotation marks? 我要在正则表达式中进行哪些更改以匹配用引号引起来的后续字符?


Edit: 编辑:

I have this conditional block that is not touched when the str value is of PRINT "Hello WOrld" , but if it has PRINT , it does. 我有一个条件块,当str值为PRINT "Hello WOrld"时未触及,但如果它具有PRINT ,则可以。

if (Regex.IsMatch(str, @"^PRINT")) {
    // some codes
}

Edit: 编辑:

If the regex would to consider the keywork, PRINT and a block of string enclosed in quotation marks, separated by a space, what is the equivalent regex for that? 如果正则表达式要考虑键盘输入, PRINT和用引号引起来的字符串块,并用空格分隔,那么等效的正则表达式是什么? Is this correct? 这个对吗?

@"^PRINT\w+\".*\""

How to delimit the quotation marks in the regex? 如何分隔正则表达式中的引号?

You can escape " with "" in verbatim string..To escape " in normal string you have to use \\" 你可以逃脱"""在逐字string..To逃生"在正常的字符串,你必须使用\\"


This would capture anything in between those two " 这样可以捕获这两个之间的任何"

if (Regex.IsMatch(str, @"^PRINT")) 
{
    Regex.Match(str,@"(?<="").*?(?="")").Value;//captures content within "
}

From what I gather you are trying to match the following list. 根据我的收集,您正在尝试匹配以下列表。 If you can be more explicit in what you are trying to match (more rules, etc) then making a regex for it should be fairly easy. 如果您可以更明确地说明要匹配的内容(更多规则等),则为其创建正则表达式将非常容易。

  • starts with PRINT 以PRINT开头
  • has 1+ non word character after 后面有1个以上的非文字字符
  • has one quote 有一个报价
  • has something (anything) after that 之后有什么(什么)
  • has one quote 有一个报价
  • end of line 行结束

You can try this: 您可以尝试以下方法:

var pattern = @"^PRINT[^\w]+""(.*)""$";  // you usually need those [] things :)

// * ""$ - requires the " to be at the end of the line
// * .* should match an empty quote ""
// you should trim the string on this one before matching

This is the test code that seems to show it working: 这是似乎可以正常工作的测试代码:

// notice that one of these has an embedded quote
var list = new [] { "PRINT", "PRINT ", "PRINT \"STUFF\"", "PRINT\t  \t\"AND \"FUN \"", " PRINT \"BAD\" " };
var pattern = @"^PRINT[^\w]+""(.*)""$";
foreach(var value in list) {
    var m = Regex.Match(value, pattern);
    if (m.Success) {
        Console.WriteLine("success: '{0}' found '{1}'", value, m.Groups[1].Value);
    } else {
        Console.WriteLine("failed:  '{0}'", value);
    }
}

And the results: 结果:

failed:  'PRINT'
failed:  'PRINT '
success: 'PRINT "STUFF"' found 'STUFF'
success: 'PRINT      "AND "FUN "' found 'AND "FUN '
failed:  ' PRINT "BAD" '

If your string str contains any whitespace or invisible character just before PRINT then Regex.IsMatch(str, @"^PRINT") will return false , use Regex.IsMatch(str, @"^.*PRINT") instead. 如果您的字符串strPRINT之前包含任何空格或不可见字符,则Regex.IsMatch(str, @"^PRINT")将返回false ,请改用Regex.IsMatch(str, @"^.*PRINT")

Example to extract Hello WOrld from input string: 从输入字符串中提取Hello WOrld示例:

string strInput = @" PRINT ""Hello WOrld""";
string pattern = @"^.*PRINT\s*""(?<keyword>(\s*\S+\s*)+)""";

if (Regex.IsMatch(strInput, pattern)) {
    Match m = Regex.Match(strInput, pattern);
    string extractedValue = m.Groups["keyword"].Value;
}

In verbatim string, use double quotes "" to escape single quote " 在逐字字符串中,使用双引号""转义单引号"

Regex.IsMatch(str,@“ \\ bPRINT \\ b”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM