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

Java 正则表达式从复杂字符串中提取子字符串,java,regex,string,java-8,substring,Java,Regex,String,Java 8,Substring,我有一个字符串,可以有两种格式 第一种格式: route-policy testPolicy if (destination in pfx_manju) then set extcommunity rt (10429:103) additive endif end-policy route-policy testPolicy if (destination in EXP1) then set extcommunity rt (27699:352002, 2.2.2.

我有一个字符串,可以有两种格式

第一种格式:

route-policy testPolicy
  if (destination in pfx_manju) then
    set extcommunity rt (10429:103) additive
  endif
end-policy
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
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
  pfx_manju) then
    set extcommunity rt (10429:103) additive
pfx_manju) or (destination in EXP4) then
        set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive
第二种格式:

route-policy testPolicy
  if (destination in pfx_manju) then
    set extcommunity rt (10429:103) additive
  endif
end-policy
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
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
  pfx_manju) then
    set extcommunity rt (10429:103) additive
pfx_manju) or (destination in EXP4) then
        set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive
第三种格式:

route-policy testPolicy
  if (destination in pfx_manju) then
    set extcommunity rt (10429:103) additive
  endif
end-policy
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
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
  pfx_manju) then
    set extcommunity rt (10429:103) additive
pfx_manju) or (destination in EXP4) then
        set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive
所以这里完整的文本是字符串。字符串可以是单个条件(if)或多个条件(elseIf条件)

从上面的字符串中,我想提取一个硬编码策略(pfx_manju)的rt值。我可以用下面的正则表达式提取rt值

final String regex = "rt \\(([^)]+)\\)";
现在的问题是,我想提取属于硬编码策略(pfx_manju)的子字符串

因此,条件是获取一个子字符串,起始位置是索引pfx_manju,结束位置是后续的endifelseif

所以我想要上面例子的sub-sting输出,如下所述

第一个子字符串:

route-policy testPolicy
  if (destination in pfx_manju) then
    set extcommunity rt (10429:103) additive
  endif
end-policy
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
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
  pfx_manju) then
    set extcommunity rt (10429:103) additive
pfx_manju) or (destination in EXP4) then
        set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive
第二个子字符串:

pfx_manju) then
        set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive
第三个子字符串:

route-policy testPolicy
  if (destination in pfx_manju) then
    set extcommunity rt (10429:103) additive
  endif
end-policy
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
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
  pfx_manju) then
    set extcommunity rt (10429:103) additive
pfx_manju) or (destination in EXP4) then
        set extcommunity rt (27699:339600, 27679:339700, 1.1.1.1:6763, 65536:45633) additive

任何带有乐观解决方案的建议都将不胜感激

以下正则表达式应该有效

pfx_manju\)[\s\S]*?rt \(([^)]+)\) additive
正则表达式匹配从
pfx_manju
条件开始一直到
rt
值的字符串,这意味着它在只有
pfx_manju
条件时捕获rt值。如果您使用的是java,则需要scape,
\


请参见regex

是您给定的子字符串,还是要提取的字符串?是。。我提到的字符串是我应该提取的Cary Swoveland,是的,字符串将始终包含一个带有“prx_manju”的条件,如果字符串不包含“prx_manju”,我将不在这里处理任何事情。好的,我猜字符串总是有条件
pfx_manju
,正如您所提到的那样。用所有可能的方法更新了问题,并添加了注释“是”,尝试了您的建议。感谢您的时间和投入