简体   繁体   English

如何使用Regex从字符串中提取值

[英]How do I extract the value from a string using Regex

I'm trying to extract the number 9 from following string using regex; 我正在尝试使用正则表达式从以下字符串中提取数字9;

" john ?\\n\\r\\n\\rFRED: 9" “约翰?\\ n \\ r \\ n \\ rFRED:9”

My regex is 我的正则表达式是

" john \\?[\\n\\r]+FRED: (.*)" “约翰\\?[\\ n \\ r] + FRED:(。*)”

The \\n and \\r are newline and return characters (white space) and bbviously "(.*) is the capturing group. \\ n和\\ r是换行符,并返回字符(空格),显然,“(。*)是捕获组。

Its not working though! 它不起作用! I'm guessing because of something really obvious. 我猜是因为确实很明显。

By the way I know the obvious way to do this is to just search the string for the ":" and then get the number that way but I'm learning regex and I've spent quite a bit of time on this and it would be good to know what I'm doing wrong. 顺便说一下,我知道执行此操作的明显方法是只在字符串中搜索“:”,然后以这种方式获取数字,但是我正在学习正则表达式,因此我花了很多时间,因此很高兴知道我在做什么错。

You need to correctly escape control characters inside your regex. 您需要正确地在正则表达式中转义控制字符。 [\\n\\r] in john \\?[\\n\\r]+FRED: (.*) is matching newline and carriage return . [\\n\\r]john \\?[\\n\\r]+FRED: (.*)是匹配newlinecarriage return Probably what you want is \\\\r\\\\n , so you can match it in the string. 可能您想要的是\\\\r\\\\n ,因此您可以在字符串中进行匹配。

The correct regex should be: john \\?[\\\\n\\\\r]+FRED: (.*) 正确的正则表达式应为: john \\?[\\\\n\\\\r]+FRED: (.*)

This is a great tool where you can visualize regex: https://regex101.com/r/qaRy5Z/1/ 这是一个很好的工具,您可以在其中可视化正则表达式: https : //regex101.com/r/qaRy5Z/1/

EDIT: After comment suggestion, you can also use raw string literal so you can omit double backslashing: 编辑:评论建议后,您还可以使用原始字符串文字,因此您可以省略双反斜杠:

 std::regex re(R"( john \?[\n\r]+FRED: (.*))");

https://gcc.godbolt.org/z/U2AbTb https://gcc.godbolt.org/z/U2AbTb

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

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