为什么我的表达式不处理多行?

时间:2015-05-05 16:06:04

标签: regex visual-studio-2010 replace

输入

以下代码是c ++中对话框的资源声明

    LTEXT           "Width",IDC_WIDTH_TEXT,203,74,22,10
    EDITTEXT        IDC_WIDTH_IN,244,73,57,12,ES_AUTOHSCROLL | WS_GROUP
    CONTROL         "Manually scale instances and paper",IDC_RAD_PSCALE_KEYIN,
    "Button",BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,15,89,132,10
    CONTROL         "Keep drawing instance scale 1.0",IDC_RAD_PSCALE_AUTO,
    "Button",BS_AUTORADIOBUTTON,15,104,123,10
    CONTROL         "Keep drawing paper scale 1.0",IDC_RAD_ISCALE_AUTO,
    "Button",BS_AUTORADIOBUTTON,15,119,118,10

期望的输出

我想使用Visual Studio 2010查找/替换对话框来处理该信息。

我想从该声明中提取所有ID并有一个清除列表,所以使用该输入我想得到这个输出:

IDC_WIDTH_TEXT
IDC_WIDTH_IN
IDC_RAD_PSCALE_KEYIN
IDC_RAD_PSCALE_AUTO
IDC_RAD_ISCALE_AUTO

1°尝试

如果我使用.*{IDC:i*}.*,那么我可以获得所有这些ID,但我不会从中获取多行部分,如果我将\1放在替换字段中,这就是输出:

IDC_WIDTH_TEXT
IDC_WIDTH_IN
IDC_RAD_PSCALE_KEYIN
    "Button",BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,15,89,132,10
IDC_RAD_PSCALE_AUTO
    "Button",BS_AUTORADIOBUTTON,15,104,123,10
IDC_RAD_ISCALE_AUTO
    "Button",BS_AUTORADIOBUTTON,15,119,118,10

2°尝试

如果我使用.*{IDC:i*}.*\n.*~({IDC:i*}),我会得到以下缺少IDC_WIDTH_IN

的输出
IDC_WIDTH_TEXT
IDC_RAD_PSCALE_KEYIN
IDC_RAD_PSCALE_AUTO
IDC_RAD_ISCALE_AUTO

如何正确获取所需的输出?

1 个答案:

答案 0 :(得分:1)

如果没有特定于语言/程序的dotall修饰符,则dot通常会匹配除换行符之外的所有内容。

请尝试此操作(demo)。请注意,我的演示中的替换将\n添加到末尾,否则它也会处理换行符并将所有内容放在一行上。

^.*?(IDC\w*)[\s\S]*?(?:$|(,\n.*$))(\n|$)

说明:

 ^                  # Anchors to the beginning to the string.
 .*?                # . denotes any single character, except for newline
                      # * repeats zero or more times
                      # ? as few times as possible
 (                  # Opens CG1
     IDC            # Literal IDC
     \w*            # Token: \w (a-z, A-Z, 0-9, _)
                      # * repeats zero or more times
 )                  # Closes CG1
 [\s\S]*?           # Character class (any of the characters within)
                      # A character class and negated character class, common expression meaning any character.
 (?:                # Opens NCG
     $              # Anchors to the end to the string.
 |                  # Alternation (NCG)
     (              # Opens CG2
         ,          # Literal ,
         \n         # Token: \n (newline)
         .*         # . denotes any single character, except for newline
         $          # Anchors to the end to the string.
     )              # Closes CG2
 )                  # Closes NCG
 (                  # Opens CG3
     \n             # Token: \n (newline)
 |                  # Alternation (CG3)
     $              # Anchors to the end to the string.
 )                  # Closes CG3