简体   繁体   English

使用PCRE Regex将JSON值与特殊字符匹配

[英]Matching JSON value with special characters with PCRE Regex

I am trying to match the value for field password with a PCRE Regex (for use with ModSecurity). 我正在尝试使用PCRE正则表达式(与ModSecurity一起使用)匹配字段密码的值。

Example input string: 输入字符串示例:

{"username":"someuser","password":"$VS*'egrE"^87Me?.?vIiJ`+"}"}}"}"}

Expected Match: 预期匹配:

$VS*'egrE"^87Me?.?vIiJ`+"}"}}"}

My broken PCRE Regex (it matches two groups instead of just the part I want): 我坏掉的PCRE正则表达式(它与两组匹配,而不仅仅是我想要的部分):

^\{(?:.*)"password":"(.*?)\"\}$

正则表达式可视化

Debuggex Demo Debuggex演示

Or am I totally wrong and it is matching correctly? 还是我完全错了并且匹配正确?

Where is the password supposed to end? 密码应该在哪里结束? Your example looks like a JSON object to me, but then you must ensure that no unescaped " are in there which is the case in your example. 在我看来,您的示例看起来像一个JSON对象,但是您必须确保其中没有未转义的"

Your regex is matching everything until the last "} which is what you expected it to match. 您的正则表达式将匹配所有内容,直到最后一个"}与您期望的匹配为止。

If you just want to match the value of the password property, you could simple use: ^.*"password":"([^"]*)".*$ 如果只想匹配password属性的值,则可以简单地使用: ^.*"password":"([^"]*)".*$

But keep in mind that this only works if there are no " within your password! 但是请记住,这仅在密码中没有" !”的情况下有效!

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

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