简体   繁体   English

在java中用字符串替换正则表达式

[英]In java replace the regex with string

In java I am using regex \\".*?\\" .在java中,我使用正则表达式\\".*?\\" I used this for replacing all the string with doublequote with a term String.我用它来用一个术语字符串替换所有带有双引号的字符串。

Ex:
INPUT: Functions.unescapeJson("test")
Result : Functions.unescapeJson("String") 

But now I wanted to exclude some string if they contains double quote.但是现在我想排除一些包含双引号的字符串。 So, I am using / as the escape character.所以,我使用/作为转义字符。 How to achieve this.如何实现这一目标。

Ex:
INPUT: Functions.getJsonPath(Functions.getJsonPath(Functions.getJsonPath(Functions.unescapeJson("test"), "m2m:cin.con"),"payloads_ul.dataFrameOutput"),"[/"Dimming Value/"]")

RESULT: Functions.getJsonPath(Functions.getJsonPath(Functions.getJsonPath(Functions.unescapeJson(String), String),String),String)

But the result I am getting if I use the previous regex is:但是如果我使用以前的正则表达式,我得到的结果是:

Functions.getJsonPath(Functions.getJsonPath(Functions.getJsonPath(Functions.unescapeJson(input.mIntegerm/:sgn.nev.rep), String),String),StringDimming ValueString)

How to achieve this using regex if it finds / it should neglect without replacing original string.如果找到/它应该忽略而不替换原始字符串,如何使用正则表达式实现这一点。

The code that I am using我正在使用的代码

public static void main(String[] args) {
    String STRINGVALIDATIONREGEX = "\".*?\"";
    String formula = "Functions.getJsonPath(Functions.getJsonPath(Functions.getJsonPath(Functions.unescapeJson(input.m2m/:sgn.nev.rep), \"m2m:cin.con\"),\"payloads_ul.dataFrameOutput\"),\"[\"Dimming Value\"]\")";
    System.out.println(formula.replace(STRINGVALIDATIONREGEX, "String"));
}

您可以使用此正则表达式:

\"(\/?.)*?\"

Use [^/] to match anything that is not a slash.使用 [^/] 匹配不是斜杠的任何内容。

For example, [^/]?\\".*[^/]?\\" would catch quotes not preceded by /例如, [^/]?\\".*[^/]?\\" 将捕获前面没有 / 的引号

"((?:[^"]|(?<=\/)")*)"
  1. " match a " "匹配一个"
  2. [^"] match a non-quote character [^"]匹配一个非引号字符
  3. | or或者
  4. (?<=\\/)") a quote character that is preceded by a / (?<=\\/)")前面有/的引号字符
  5. * match sub-expressions 2 - 4 zero or more times. *匹配子表达式 2 - 4 次或多次。
  6. " match a " "匹配一个"

See Regex demo请参阅正则表达式演示

If you believe that a string such as "abc/" is invalid, then you should use the stricter regex:如果您认为诸如"abc/"类的字符串无效,那么您应该使用更严格的正则表达式:

"((?:[^"\/]|\/")*)"
  1. " match " "匹配"
  2. [^"\\/] match a any character that isn't a quote for / [^"\\/]匹配任何不是/引号的字符
  3. | or或者
  4. \\/" match a /" combination \\/"匹配一个/"组合
  5. * match sub-expressions 2 - 4 zero or more times. *匹配子表达式 2 - 4 次或多次。
  6. " match a " "匹配一个"

See Regex demo请参阅正则表达式演示

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

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