[英]Extract comma separated units from source code using RegEx
I want to use Regular Expressions to extract information from my source code.我想使用正则表达式从我的源代码中提取信息。 Can you help me to build a RegEx that retrieves the units used on the source code?.你能帮我构建一个 RegEx 来检索源代码中使用的单位吗?
Source code sample:源代码示例:
unit ComandesVendes;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Manteniment;
type
TFComandesVendes = class(TFManteniment,ActualitzacioFinestra)
QRCapsaleraNumero: TIntegerField;
QRCapsaleraData: TDateTimeField;
QRCapsaleraDataEntrega: TDateTimeField;
...
...
I need to get the comma-separated file names since the uses
clause up to the next ;
我需要获取逗号分隔的文件名,因为uses
子句到下一个;
. . In that sample the output must be:在该示例中,output 必须是:
Windows
Messages
SysUtils
Variants
Classes
Graphics
Controls
Forms
Dialogs
Manteniment
I'm trying something like我正在尝试类似的东西
^ *uses(\n* *(\w*),)* *\n* *(\w*) *;
It matches the uses clause, but it doesn't return each file name separately.它匹配 uses 子句,但它不会分别返回每个文件名。
Thank you.谢谢你。
At this page it says that Delphi uses the PCRE regex flavor.在此页面上,它说 Delphi 使用 PCRE 正则表达式风格。
In that case, one option is to use a capturing group in combination with the \G
anchor.在这种情况下,一种选择是结合使用捕获组和\G
锚点。
(?:^ *uses\r?\n *|\G(?!^))(\w+)(?:,\s*|;$)
Explanation解释
(?:
Non capture group (?:
非捕获组
^ *uses\r?\n *
Match optional spaces from the start of the string, then match and a newline followed by optional spaces again ^ *uses\r?\n *
从字符串的开头匹配可选空格,然后再次匹配一个换行符,后跟可选空格|
Or要么\G(?!^)
Assert the position at the end of the previous match, not at the start (The \G
anchor matches at 2 positions, either at the start of the string or at the end of the previous match) \G(?!^)
在上一场比赛结束时断言 position,而不是在开始( \G
锚点匹配 2 个位置,要么在字符串的开头,要么在上一场比赛的结尾))
Close non capture group )
关闭非捕获组(\w+)
Capture group 1 Match 1+ word characters (\w+)
捕获组1匹配1+个单词字符(?:,\s*|;$)
Non capture group, match either a comma and 0+ whitespace chars or match ;
(?:,\s*|;$)
非捕获组,匹配逗号和 0+ 个空白字符或匹配;
at the end of the string.在字符串的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.