你没有指定你正在使用的是什么正则表达式引擎,但是如果你有前瞻/后顾之处,这个工程就可以工作。
它的工作原理是键只有全部大写,而值不是 - 不知道这是否是一个有效的假设,但如果它不是那么如指出的事情会变得复杂和混乱。
(?<=[+-\/])[A-Z]+=(?:(?![A-Z]+=)[^=])+(?=[+-\/]|$)
这里是我试图解释(不知道有多少这是有道理的):
(?x) # enable regex comment mode
(?<=[+-\/]) # start with one of the delimiters, but excluded from match
[A-Z]+ # match one or more uppercase (for the key)
= # match the equal sign
(?: # start non-capturing group
(?! # start negative lookahead, to prevent keys matching
[A-Z]+= # a key and equals (since in negative lookahead, this is what we exclude)
) # end the negative lookahead
[^=] # match a character that's not =
)+ # end non-capturing group, match one or more times, until...
(?=[+-\/]|$) # next char must be delimiter or end of line for match to succeed
对于Java与字符串>正则表达式,反斜杠需要转义(如果有的话):
Pattern p = Pattern.compile("(?<=[+-\\/])[A-Z]+=(?:(?![A-Z]+=)[^=])+(?=[+-\\/]|$)");
如果需要捕获组,只需加括号一轮的适当部位:
Pattern p = Pattern.compile("(?<=[+-\\/])([A-Z]+)=((?:(?![A-Z]+=)[^=])+(?=[+-\\/]|$))");
的这一匹配的部分,把它变成新行分隔符的文本,就像...
Matcher m = p.Matcher(InputText);
StringBuffer Result = new StringBuffer("");
while (m.find())
{
Result.append(m.Group() + "\n");
}
当你的数据值可以包含你的分隔符时,正则表达式会大大地使情况复杂化。你对数据是如何控制的? – 2010-06-15 17:13:23
不幸的是,这些数据来自我无法控制的许多不同来源。 – 2010-06-15 17:15:16
可能更容易找到你正在寻找的密钥,而不是分离器?寻找'用户','组'和'功能'并解析相应的结果? – AllenG 2010-06-15 17:18:48