Java 如何使用正则表达式替换编码字符串中的:字符?
我有一个带语法的编码字符串Java 如何使用正则表达式替换编码字符串中的:字符?,java,regex,Java,Regex,我有一个带语法的编码字符串 "encodedProp:encodedValue OPERATOR encodedProp1:encodedValue1" (运算符可能是和,或,不是,并且有N对属性:值) “encodedProp”,“encodedValue”,“encodedProp1”,“encodedValue1”。。。是经过编码的字符串 我想使用正则表达式将“:”替换为“=”。此外,“:”左侧的部分应替换为“\”“+left\u part+”\”“,右侧的部分应替换为““+right\
"encodedProp:encodedValue OPERATOR encodedProp1:encodedValue1"
(运算符可能是和
,或
,不是
,并且有N对属性:值
)
“encodedProp”
,“encodedValue”
,“encodedProp1”
,“encodedValue1”
。。。是经过编码的字符串
我想使用正则表达式将“:”
替换为“=”
。此外,“:”
左侧的部分应替换为“\”“+left\u part+”\”“
,右侧的部分应替换为““+right\u part+”
在上述示例中,替换后的字符串应为:
"\"encodedProp\" = 'encodedValue' OPERATOR \"encodedProp1\" = 'encodedValue1'"
要做到这一点,我必须使用什么表达式?好的,我在这里做一个简单的解释,因为这个问题没有很好的定义,但是让我们来试试
String resultString = subjectString.replaceAll(
"(?x)( # Match and capture in backreference number 1:\n" +
" [^\\s:]+ # one or more characters except spaces or colons\n" +
") # End of capturing group 1\n" +
": # Match a colon\n" +
"( # Match and capture in backreference number 2:\n" +
" [^\\s:]+ # one or more characters except spaces or colons\n" +
") # End of capturing group 2",
"\"$1\" = '$2'");
这完全忽略了
运算符
部分-它只会查找包含一个冒号的字符序列,并将它们用单引号/双引号括起来,同时替换冒号。您的问题的格式肯定可以稍微好一点吗?也很难真正说出您想要什么。不要只要求我们为您编写代码。向我们展示您已经拥有的相关代码片段,并询问有关您需要帮助的具体问题。需要澄清:1)字符串中是否始终有2个prop/value对?2) 运算符是否总是“和”可以编码值包含“:”我使用了regex:(?I)(?)?