Notepad ++ RegEx:在后续行中搜索两个单词

时间:2015-12-15 09:17:20

标签: regex notepad++

考虑以下文字

-- 1.3.6.1.4.1.16213.2.3.1.2
    xfsCDMStatusTable OBJECT-TYPE
        SYNTAX SEQUENCE OF XfsCDMStatusEntry
        MAX-ACCESS not-accessible
        STATUS current
        DESCRIPTION
            "Define the set of MIB Variables for the CDM status table."
        ::= { xfsCDMV1 2 }


    -- 1.3.6.1.4.1.16213.2.3.1.2.1
    xfsCDMStatusEntry OBJECT-TYPE
        SYNTAX XfsCDMStatusEntry
        MAX-ACCESS not-accessible
        STATUS current

需要过滤第一行包含“1.3.6.1.4.1.16213”和下一行包含“OBJECT-TYPE”的行。

输出如

 -- 1.3.6.1.4.1.16213.2.3.1.2
    xfsCDMStatusTable OBJECT-TYPE
 -- 1.3.6.1.4.1.16213.2.3.1.2.1
    xfsCDMStatusEntry OBJECT-TYPE

有人可以帮助Regex模式。

2 个答案:

答案 0 :(得分:1)

试试这个表达式:

(?-s)^(?:(?:(?!.*?\R+.*?\bOBJECT-TYPE\b).*?$(?:\R+|$))|(^.*?\R+.*?\bOBJECT-TYPE\b.*?$))

替换为$1

这需要输入:

-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }


-- 1.3.6.1.4.1.16213.2.3.1.2.1
xfsCDMStatusEntry OBJECT-TYPE
    SYNTAX XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current


-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }

并输出以下内容:

-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
-- 1.3.6.1.4.1.16213.2.3.1.2.1
xfsCDMStatusEntry OBJECT-TYPE
-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE

如果您只想指定值为1.3.6.1.4.1.16213的行,请使用以下内容:

(?-s)^(?:(?:(?!.*?1\.3\.6\.1\.4\.1\.16213.*?\R+.*?\bOBJECT-TYPE\b).*?$(?:\R+|$))|(^.*?\R+.*?\bOBJECT-TYPE\b.*?$))

答案 1 :(得分:0)

使用这个:

  • 查找内容:(\-\- 1\.3\.6\.1\.4\.1\.16213\b[^\r\n]*[\r\n]+[^\r\n]*OBJECT-TYPE[^\r\n]*)([\r\n]+ [^\r\n]*)*[\r\n]*
  • 替换为:$1\n
  • 选中“正则表达式”
  • “。匹配换行符”未选中

内容示例:

-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }


-- 1.3.6.1.4.1.16213.2.3.1.2.1
xfsCDMStatusEntry OBJECT-TYPE
    SYNTAX XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current


-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }


-- 1.3.6.1.4.1.xxxxx.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }

[全部替换]后:

-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
-- 1.3.6.1.4.1.16213.2.3.1.2.1
xfsCDMStatusEntry OBJECT-TYPE
-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
-- 1.3.6.1.4.1.xxxxx.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }