简体   繁体   English

正则表达式在C代码中提取字符串(不在注释中)

[英]Regular expression to extract string in C Code (not inside comment)

I have this code in C but I only know how to extract string with regular expression that not inside comment code: 我在C中有这个代码,但我只知道如何使用不在注释代码中的正则表达式提取字符串:

1. /*  * "path_build()" function in "home.c" for more information.  
2. * this is an example basic"
3. */
4.
5. /*** Free ***/ 
6. VALOR = string_make(format("%sxtra", libpath)); 
7. event_signal_string(EVENT_INITSTATUS, "Inicializando...");

should only return: 应该只返回:

"%sxtra" 
"Inicializando..."

I try: 我尝试:

".*"

but its don't work, it show me all text inside "", including the strings that inside /*...*/ 但是它不起作用,它向我显示“”里面的所有文字,包括里面的字符串/*...*/

I use EditPag Pro, RegExp panel. 我使用EditPag Pro,RegExp面板。 It's a game translation project, I take the string of every C file and I translate to Spanish. 这是一个游戏翻译项目,我拿每个C文件的字符串,我翻译成西班牙语。 I can't remove the comments of the original file. 我无法删除原始文件的注释。

The only thing I have clear is that this is the regex to find comments in C, maybe that will help the solution: 我唯一清楚的是,这是在C中查找注释的正则表达式,也许这将有助于解决方案:

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)

Any help? 有帮助吗?

Edit: I put number of lines. 编辑:我把行数。

Hernaldo, this is an interesting question. 赫纳尔多,这是一个有趣的问题。

Here are two versions because I am not sure if you want to capture the " inside of the string " or "the whole string" 这里有两个版本,因为我不确定你是否要捕获“ inside of the string ”或"the whole string"

The regexps below capture the strings to capture Group 1. You completely ignore the overall match (Group 0) and just focus on Group 1. To retrieve the strings, just iterate over Group 1 matches in your language (discarding empty strings if any). 下面的正则表达式捕获字符串以捕获组1.您完全忽略整体匹配(组0)并只关注组1.要检索字符串,只需迭代您的语言中的组1匹配(如果有的话,丢弃空字符串)。

Version 1: " The inside of the string " 版本1:“ The inside of the string

(?s)/\*.*?\*/|"([^"]+)"

This will capture %sxtra and Inicializando... to Group 1. 这将捕获%sxtraInicializando...到组1。

Version 2: "The whole string" 版本2: "The whole string"

(?s)/\*.*?\*/|("[^"]+")

This will capture "%sxtra" and "Inicializando..." to Group 1. 这将捕获"%sxtra""Inicializando..."到第1组。

Please let me know if you have any questions! 请让我知道,如果你有任何问题!

Note: I did not handle /* nested /* comments */ */ as that was not specified in the question. 注意:我没有处理/* nested /* comments */ */ ,因为问题中未指定。 That would require a bit of tweaking and probably a regex engine supporting recursion. 这需要一些调整,可能需要一个支持递归的正则表达式引擎。

The final solution for EditPad 6/7 is: EditPad 6/7的最终解决方案是:

(?<!^[ \t]*/?[*#][^"\n]*")(?<=^[^"\n]*")[^"]+

Link: Regular expression for a string that does not start with a /* 链接: 不以/ *开头的字符串的正则表达式

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

相关问题 用C清理正则表达式代码? - Cleaner regular expression code in C? 有没有办法从 C 字符串中提取注释? - Is there a way to extract a comment out of a C string? Linux命令中的C代码:/ *是否视为注释? - Linux command inside C code: /* treat as comment? 在C中使用带有unicode字符串的正则表达式 - using regular expression with unicode string in C Lex / Flex:C / C ++中字符串文字的正则表达式? - Lex/Flex :Regular expression for string literals in C/C++? 使用正则表达式查找 C 样式注释块 (/* */) 但不是字符串内部的注释块? - Finding a C style comment block (/* */) but not those inside of a string using regex? 供应代码正则表达式 - Provisioning code regular expression 字符串/正则表达式字符&#39;[&#39;,&#39;]&#39;,&#39;{&#39;,&#39;}&#39;用C语言在大型机TN3270(代码页1047,1147,500,249)上替换为空格 - String/Regular expression characters '[', ']', '{', '}' replaced by spaces on Mainframe TN3270 (with code page 1047,1147,500,249) in C language 正则表达式,用于在C中的随机字符串中查找两位数字 - Regular expression to find two-digit numbers in a random string in C 如何使用正则表达式检测无效的C转义字符串? - How to detect an invalid C escaped string using a regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM