示例php regex(下面)使用子例程调用来工作.
如果我尝试将它与C#Regex类一起使用,我会收到一个错误: Unrecognized grouping construct
是否可以将其重写为C#regex语法?
它是一个简单的翻译,还是需要使用另一种(正则表达式)方法?
如果它不可能是它所使用的东西的名称是什么,那么我可以将它添加到这个问题中,以使其对具有相同问题的其他人更有用吗?
$pcre_regex = '
/
(?(DEFINE)
(?<number> -? (?: [1-9]\d*| 0 ) (\.\d+)? (e [+-]? \d+)? )
(?<boolean> true | false | null )
(?<string> " (?>[^"\\\\]+ | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* " )
(?<array> \[ (?: (?&json) (?: , (?&json) )* )? \s* \] )
(?<pair> \s* (?&string) \s* : (?&json) )
(?<object> \{ (?: (?&pair) (?: , (?&pair) )* )? \s* \} ) …Run Code Online (Sandbox Code Playgroud) 我有一个场景,我需要验证部分输入(见下文)是否是有效的 JSON?我引用了这个答案来确定给定的字符串是否是有效的 JSON。
输入示例:
{
"JSON": [{
"foo":"bar",
"details": {
"name":"bar",
"id":"bar",
Run Code Online (Sandbox Code Playgroud)
到目前为止我已经尝试过:
/ (?(DEFINE)
(?<number> -? (?= [1-9]|0(?!\d) ) \d+ (\.\d+)? ([eE] [+-]? \d+)? )
(?<boolean> true | false | null )
(?<string> " ([^"\n\r\t\\\\]* | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* " )
(?<array> \[ (?: (?&json) (?: , (?&json) )* )? \s* \]{0,1} )
(?<pair> \s* (?&string) \s* : (?&json) )
(?<object> \{ (?: (?&pair) (?: , (?&pair) )* )? \s* \}{0,1} )
(?<json> …Run Code Online (Sandbox Code Playgroud) 这是我的测试类,我正在尝试单元测试我的方法createaccount()
class CreateAccountTest1(unittest.TestCase):
def testCreateAccount_1(self,data):
text = "{'user_id':'abc123','action':'add','names':['hello','world']}"
regex = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.| [^"\\])*"/ g, ''))) && eval('(' + text + ')')
self.assertRegexpMatches(text, reg, 'my msg')
Run Code Online (Sandbox Code Playgroud)
createaccount()方法是
class CreateAccountClass():
def CreateAccount(self,data):
Run Code Online (Sandbox Code Playgroud)
现在我必须检查是否createaccount()是json格式的参数.
如果我通过 data=
{ "_id" : "user@gmail.com", "H_id" : "smsg0", "name" : "vish", "passwrd" : "xj45cd" }
Run Code Online (Sandbox Code Playgroud)
它应该检查它是否是json,我相信这是json格式.现在在我的方法中createaccount()它应该检查是否data是json格式,如果不是它应该打印错误信息,如果它适用regex?或任何建议,谢谢,
我已经和JSON合作了很长一段时间了.
我知道JSON是stringifiedJavaScript Object.
我也知道是什么使JSON,如下面的线程中所讨论的.
JSON和Object Literal Notation有什么区别?
问题: 但是,我想知道是否有typeof某种方式可以告诉我给定的字符串是JSON还是任何其他字符串.
到目前为止我所观察到的:
1. var data = {"name":"JS"};
alert(typeof(data)); // object. Agree!
2. // I know this is JSON, by looking at the syntax
var data = '{"name":"JS"}';
alert(typeof(data)); // string.
3. // I know this is just any other string (NOT JSON), by looking at the syntax.
var data = "AnyOtherString";
alert(typeof(data)); // string
Run Code Online (Sandbox Code Playgroud)
在JavaScript中,有什么方法可以区分上面的第2 点和第3点.可能,类似的东西typeof会告诉我它是否只是一个字符串或JSON(也是一个字符串).