简体   繁体   English

编写正则表达式模式c ++

[英]writing Regex pattern c++

Im trying to write a regex pattern that will match once it reads an assortment of characters (can be any length/size) and stops once it reaches an open parenthesis, '('. 我试图编写一个正则表达式模式,该模式将在读取各种字符(可以是任意长度/大小)时匹配,并在到达开放括号'('时停止。

For example if a string contains: "INSERT((address),(phone number)) 例如,如果字符串包含:“ INSERT((地址),(电话号码))

It will match once it finds: INSERT( 找到后它将匹配:INSERT(

My regex pattern looks like: 我的正则表达式模式如下:

regex pattern1("^[.]*+\(");

What i'm trying to do here is use the '^' because it specifies a pattern must start at the beginning of a sequence. 我在这里尝试使用的是'^',因为它指定了模式必须从序列的开头开始。

[.]* to tell the computer that any character can be used any number of times. [。] *告诉计算机任何字符可以使用多次。

and then the backslash( to signify the end of the pattern. 然后使用反斜杠(表示模式结束。

This is not working, would love if someone could help! 这是行不通的,希望有人能帮助您!

The pattern you wrote matches parentese too. 您编写的模式也与父母匹配。

Look (base on your pattern): 外观(根据您的图案):

1- First you should use ^ at the beginning of your pattern to force it starts with the next character. 1-首先,您应该在模式的开头使用^强制其从下一个字符开始。 So up now: 所以现在:

^"

2- You should put an exception on parentese because . 2-您应该对父母例外,因为. means every character include parentese. 表示每个字符都包含父母。 So: 所以:

^"[^\(]*

This section of pattern [^\\(] is saying any character except parentese. 模式[^\\(]这一部分说的是除括号外的任何字符。

3- And finally you should say the matches should end with a parentese. 3-最后,您应该说比赛以父母身份结束。 So: 所以:

^"[^\(]*\(

Regex101 正则表达式101


Notice: If you don't want that " included, you should use Groups . Look at this: 注意:如果您不希望包含" ,则应使用“ Groups 。查看以下内容:

^"([^\(]*\()

Now you can access INSERT( on the second index of matched results. 现在,您可以在匹配结果的第二个索引上访问INSERT(

Regex101 正则表达式101

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

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