如何使用PHP中的正则表达式在字符串中搜索可以按任何顺序排列的子字符串(How to search a string for substrings that can be in any order using regular expressions in PHP)
我目前正在使用以下列格式输出JSON数据的Web服务:
{"ID":1291,"Name":"Houses of the Holy","Artist":"Led Zeppelin","Year":1973}
但是,从一个项目到下一个项目的顺序不一致。 例如:
{"ID":2958,"Label":"Reprise Records","Name":"Electric Ladyland","Year":1968}
当然,我不能编写依赖于统一顺序的属性的正则表达式,因为顺序因项目而异(尽管它们始终以ID开头)。
我会使用',''作为分隔符使用preg_split,但偶尔会有一个跟踪列表,如下所示:
{"ID":9237,"Tracklist":[{"Track1":"Taxman","Track2":"Eleanor Rigby"}]}
......这会弄乱它。
如何使用正则表达式搜索可能以任何顺序出现的子字符串,或者可能根本不显示? 或者我是否必须在Web服务返回的每个项目上运行多个正则表达式,每个项目对应一个可能的属性?
I'm currently working with a web service that is outputting JSON data in the following format:
{"ID":1291,"Name":"Houses of the Holy","Artist":"Led Zeppelin","Year":1973}
However, the order isn't consistent from one item to the next. For example:
{"ID":2958,"Label":"Reprise Records","Name":"Electric Ladyland","Year":1968}
Naturally I can't write a regular expression that depends on the attributes being in a uniform order, since the order varies from one item to the next (although they always start with ID).
I would use preg_split using ',"' as the delimiter, but there will occasionally be a tracklisting, like this:
{"ID":9237,"Tracklist":[{"Track1":"Taxman","Track2":"Eleanor Rigby"}]}
...which would mess it up.
How can I use a regular expression to search for substrings that may appear in any order, or perhaps not appear at all? Or will I have to run multiple regular expressions on each item the web services returns, one for each possible attribute?
原文:
最满意答案
JetBrains说,“不幸的是,没有鼠标就无法滚动控制台输出。” 我提交了一个增强请求,在Python控制台中添加键盘滚动 。
JetBrains says, "Unfortunately it's not possible to scroll console output without mouse." I filed an enhancement request to add keyboard scrolling in the Python console.
相关问答
更多-
在文件“pycharm.exe.vmoptions”中添加以下行可以解决问题: -Dfile.encoding=UTF-8 这似乎是PyCharm 5.0的一个错误...... Add following line in the file "pycharm.exe.vmoptions" can fix the issue: -Dfile.encoding=UTF-8 It seems to be a bug from PyCharm 5.0...
-
对于我来说,在控制台的左上方有一个小重新设置按钮(带有弯曲的绿色箭头的小方块),它将“重新运行”作为工具提示; 这似乎重置控制台。 For me there is a little reset button (small square with a curved green arrow) on the top left of the console that says "Rerun" as the tool tip; this seems to reset the console.
-
使用execfile('/Users/mymac/Documents/Python/test.py') 。 你正试图运行一个像可执行文件一样的python文件。 最重要的是,你正在使用python解释器。 使用python的execfile例程来从解释器调用文件,或使用python test.py从终端运行它。 use execfile('/Users/mymac/Documents/Python/test.py'). You are trying to run a python file like an ...
-
对于版本3.4.1: 查看 - >活动编辑器 - >使用软包装 I found the answer here: File -> Settings -> Editor -> "Virtual Space" group -> Use soft wraps in console In Pycharm version 2018.3.3 it can be found under File -> Settings -> Editor -> General -> Console: Use soft wraps in ...
-
PyCharm的调试控制台失败,出现KeyError错误:'_sh'(PyCharm's debugging console fails with KeyError: '_sh')[2022-01-16]
通过升级到PyCharm 2017.1.4解决了问题。 Problem solved by upgrading to PyCharm 2017.1.4. -
在PyCharm python控制台中复制scrapy shell功能(Replicate scrapy shell abilities in PyCharm python console)[2021-11-07]
我通过运行以下python片段来实现它: from scrapy.cmdline import execute import sys sys.argv = ['scrapy', 'shell', 'http://scrapy.org'] execute() 如果您收到以下错误: MultipleInstanceError: Multiple incompatible subclass instances of InteractiveShellEmbed are being created. 那么你必须在 ... -
( Spyder维护者在这里 )为了避免Spyder中的这个问题,我们一次只允许在控制台中显示500行( 注意 :此限制可由用户配置) 所以我会说Pycharm的控制台没有这个功能(虽然我无法确定)。 (Spyder maintainer here) To avoid this problem in Spyder we only allow 500 lines to be shown in the console at a time (Note: This limit is configurable by ...
-
Pycharm - Python控制台(Pycharm - Python Console)[2022-05-30]
这就是你的pydev_import_hook.py应该是这样的: import sys from _pydevd_bundle.pydevd_constants import dict_contains from types import ModuleType class ImportHookManager(ModuleType): def __init__(self, name, system_import): ModuleType.__init__(self, name) ... -
Pycharm控制台是空白的(Pycharm console is blank)[2022-03-15]
通过重新启动Pycharm,关闭所有文件,并为新项目选择文件夹而不是文件来解决此问题。 解决方案受工作目录错误启发。 这也让我可以从令人沮丧的众多可用Python安装中访问正确的一个。 This issue was solved by re-starting Pycharm, closing all files, and choosing a folder, rather than a file, for a new project. Solution inspired by Working directo ... -
JetBrains说,“不幸的是,没有鼠标就无法滚动控制台输出。” 我提交了一个增强请求,在Python控制台中添加键盘滚动 。 JetBrains says, "Unfortunately it's not possible to scroll console output without mouse." I filed an enhancement request to add keyboard scrolling in the Python console.