简体   繁体   English

正则表达式从复杂字符串中提取子字符串

[英]Regex to extract sub string from complex string

I've a string that can have in two formats,我有一个可以有两种格式的字符串,

First Format:第一种格式:

route-policy testPolicy
  if (destination in pfx_manju) then
    set extcommunity rt (10429:103) additive
  endif
end-policy

Second Format:第二种格式:

route-policy testPolicy
  if (destination in EXP1) then
    set extcommunity rt (27699:352002, 2.2.2.2:98) additive
  elseif (destination in pfx_manju) then
    set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive
  elseif (destination in EXP5) then
    drop
  endif
end-policy

Third Format:第三种格式:

route-policy EXPORTRP1
  if (destination in EXP1) or (destination in EXP2) then
    set extcommunity rt (27699:352002, 2.2.2.2:98) additive
  elseif (destination in pfx_manju) or (destination in EXP4) then
    set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive
  elseif (destination in EXP5) or (destination in EXP6) then
    drop
  endif
end-policy

So here the complete text is coming as string.所以这里完整的文本以字符串的形式出现。 The string either single condition (if) or multiple conditions (elseIf conditions).字符串可以是单个条件 (if) 或多个条件 (elseIf 条件)。

From the above string I want to extract the rt values for one hard coded policy (pfx_manju).从上面的字符串中,我想提取一个硬编码策略 (pfx_manju) 的 rt 值。 I can extract the rt values with below regex,我可以用下面的正则表达式提取 rt 值,

final String regex = "rt \\(([^)]+)\\)";

Now the problem is, I want to extract the sub string which is belongs to the hard coded policy (pfx_manju).现在的问题是,我想提取属于硬编码策略(pfx_manju)的子字符串。

So condition is get a sub string with starting position is index of pfx_manju and ending position is the subsequent endif or elseif .所以条件是得到一个子字符串,开始 position 是pfx_manju的索引,结束 position 是后续的endifelseif

So I want the sub sting output for above examples as mentioned below,所以我想要下面提到的上述示例的子字符串 output,

First Sub String:第一个子字符串:

  pfx_manju) then
    set extcommunity rt (10429:103) additive

Second Sub String:第二个子字符串:

pfx_manju) then
        set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive

Third Sub String:第三个子字符串:

pfx_manju) or (destination in EXP4) then
        set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive

Any suggestions with optimistic solution will be appreciated任何具有乐观解决方案的建议将不胜感激

The following regex should work以下正则表达式应该可以工作

pfx_manju\)[\s\S]*?rt \(([^)]+)\) additive

The regex matches string starting from pfx_manju condition all the way to rt values, that means it captures the rt values when there is only pfx_manju condition.正则表达式匹配从pfx_manju条件开始一直到rt值的字符串,这意味着它在只有pfx_manju条件时捕获 rt 值。 if you are using java, you need to scape, \ .如果您使用的是 java,则需要 scape, \

See regex demo见正则表达式演示

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

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