简体   繁体   English

如何写这个特殊的正则表达式

[英]how to write this particular regex

What is the regex I need to use if I 如果我需要使用什么正则表达式?

Need to select the "10" in player list and not the "10" from group 需要在玩家列表中选择“ 10”,而不是从组中选择“ 10”

{"group":"10","player":["12","15","13","10","1"]}

Sorry I should have given a little more on the programming scope 抱歉,我应该在编程范围上多做一些介绍

The json string is supposed to be treated as a string and not json parsed because I need to use query in mysql to select rows that match this criteria.. json字符串应该被视为一个字符串,而不是json解析的,因为我需要在mysql中使用查询来选择符合此条件的行。

for example, in the above example... 例如,在上面的示例中...

Select * from team Where jsondata=(Something to the effect that "10" can be found in player list but will ignore if "10" is found only in group)

If i use: 如果我使用:

Select * from team Where jsondata LIKE'%"10"%'

then obviously it would be wrong because if the data was {"group":"10","player":[]} it would still qualify 那么显然这是错误的,因为如果数据是{"group":"10","player":[]}它仍然符合条件

i want to run a php command like preg_match to match the "10" form the player list if found 我想运行像preg_match这样的php命令以匹配“ 10”形式的播放器列表(如果找到)

Thanks for those who have replied so far 感谢那些到目前为止的答复

Try somenthing like this: "player":\\[.*"(10)" or "player":\\[("\\d+",)*?"(10)" 尝试这样的事情: "player":\\[.*"(10)""player":\\[("\\d+",)*?"(10)"

Depends on, how strictly you like it. 取决于您有多严格。

In MySQL you can use this: 在MySQL中,您可以使用以下命令:

SELECT * FROM team WHERE jsondata REGEXP '"player":.*"10"'

I know you asked for a regular expression. 我知道你要求一个正则表达式。 However, if the string will always be a Python dict, then how about: 但是,如果字符串将始终是Python dict,那么如何:

str = "{\"group\":\"10\",\"player\":[\"12\",\"15\",\"13\",\"10\",\"1\"]}"

"10" in eval( str )["player"]

This would test whether the string "10" exists for the list in the "player" key. 这将测试"player"键中的列表是否存在字符串"10" You might want something slightly different. 您可能想要一些稍微不同的东西。 The point being, if the data will always be a serialized Python data structure, you'll be able to work with this data much more naturally as a Python data structure. 关键是,如果数据将始终是序列化的Python数据结构,那么您将能够更自然地将其作为Python数据结构使用。

Note, since I was typing the string manually I had to escape the " . If you were reading this string from a file you wouldn't have to do that. 请注意,由于我是手动键入字符串,因此必须转义" 。如果从文件中读取此字符串,则不必这样做。

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

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