如何在JSON中存储正则表达式日期模式

时间:2022-09-15 19:52:17

My code requires me to store regex string in JSON. this is working fine for most of the patterns but lands in trouble when date pattern with '/' is used

我的代码要求我在JSON中存储正则表达式字符串。这对大多数模式都很好,但是当使用带有'/'的日期模式时会遇到麻烦

i tried escaping with a '\'

我尝试用'\'逃脱

(\\d{1,2}\/\\d{1,2}\/\\d{1,2}) this seems to be working fine as JSONLint does give any error

(\\ d {1,2} \ / \\ d {1,2} \ / \\ d {1,2})这似乎工作正常,因为JSONLint确实给出了任何错误

however the challenge is when i am trying to parse the JSON string in a JAVA program it gives error as it further requires '\' and '/' to be escaped. I have tried multiple options but not able to solve

然而,挑战是当我试图解析JAVA程序中的JSON字符串时,它会产生错误,因为它还需要'\'和'/'进行转义。我尝试了多种选择,但无法解决

2 个解决方案

#1


1  

I think your proposed regex escapes a backslash too many: Have a look at: https://regex101.com/r/xBFeZG/1

我认为你提出的正则表达式反对过多的反斜杠:看看:https://regex101.com/r/xBFeZG/1

It's only the \ that needs to be escaped in java regexes, so transforming what I believe you want to that would be:

只有\需要在java正则表达式中进行转义,所以转换我认为你想要的东西是:

 (\\d{1,2}\\/\\d{1,2}\\/\\d{1,4})

However, why not simply use a standard date format (like: dd/MM/yyyy -> see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) and do something like:

但是,为什么不简单地使用标准日期格式(例如:dd / MM / yyyy - >请参阅https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html)和做类似的事情:

LocalDate.parse(date, DateTimeFormatter.ofPattern(format)

#2


0  

If you have an expression like

如果你有一个表达式

\d{1,2}/\d{1,2}/\d{1,4}

then exporting it as JSON will produce something like this

然后将其导出为JSON将生成这样的东西

{ "regex": "\\d{1,2}\/\\d{1,2}\/\\d{1,4}" }

with every "\" being escaped as "\\".

每个“\”被转义为“\\”。

To parse correctly in Java, you really just have to "un-escape" the escaped backslashes, in other words, remove the leading backslash. Something like this should work:

要在Java中正确解析,您实际上只需要“解除”转义的反斜杠,换句话说,删除前导反斜杠。像这样的东西应该工作:

String regex = jsonRegex.replaceAll("\\\\(.)", "$1");

EDIT: Forward slashes don't actually need to be escaped, although escaping them doesn't hurt. So, the expression will most probably be emitted in JSON like

编辑:正斜杠实际上不需要被转义,虽然逃避它们不会受到伤害。因此,表达式最有可能以JSON格式发出

\\d{1,2}/\\d{1,2}/\\d{1,4}

#1


1  

I think your proposed regex escapes a backslash too many: Have a look at: https://regex101.com/r/xBFeZG/1

我认为你提出的正则表达式反对过多的反斜杠:看看:https://regex101.com/r/xBFeZG/1

It's only the \ that needs to be escaped in java regexes, so transforming what I believe you want to that would be:

只有\需要在java正则表达式中进行转义,所以转换我认为你想要的东西是:

 (\\d{1,2}\\/\\d{1,2}\\/\\d{1,4})

However, why not simply use a standard date format (like: dd/MM/yyyy -> see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) and do something like:

但是,为什么不简单地使用标准日期格式(例如:dd / MM / yyyy - >请参阅https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html)和做类似的事情:

LocalDate.parse(date, DateTimeFormatter.ofPattern(format)

#2


0  

If you have an expression like

如果你有一个表达式

\d{1,2}/\d{1,2}/\d{1,4}

then exporting it as JSON will produce something like this

然后将其导出为JSON将生成这样的东西

{ "regex": "\\d{1,2}\/\\d{1,2}\/\\d{1,4}" }

with every "\" being escaped as "\\".

每个“\”被转义为“\\”。

To parse correctly in Java, you really just have to "un-escape" the escaped backslashes, in other words, remove the leading backslash. Something like this should work:

要在Java中正确解析,您实际上只需要“解除”转义的反斜杠,换句话说,删除前导反斜杠。像这样的东西应该工作:

String regex = jsonRegex.replaceAll("\\\\(.)", "$1");

EDIT: Forward slashes don't actually need to be escaped, although escaping them doesn't hurt. So, the expression will most probably be emitted in JSON like

编辑:正斜杠实际上不需要被转义,虽然逃避它们不会受到伤害。因此,表达式最有可能以JSON格式发出

\\d{1,2}/\\d{1,2}/\\d{1,4}