省略第一个匹配的元素regex python [已关闭]

atmip9wb  于 2022-12-14  发布在  Python
关注(0)|答案(2)|浏览(142)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

7天前关闭。
Improve this question
很抱歉打错了。希望问题得到澄清。我的字符串看起来像这样:

"{Published by: THQ Inc.}, {Developed by: Heavy Iron Studios, Inc.}, {Country: United States}, {Release Date: Nov 01, 2005}, {UPC-A: 7xa085138xa038046xa09}, {Nintendo Media PN: DL-DOL-GIQE-USA}, {Country: France}, {Release Date: Nov 11, 2005}, {EAN-13: 4xa0005209xa0068024}, {Country: United Kingdom}, {Release Date: Nov 11, 2005}, {Country: Germany}, {Release Date: Nov 11, 2005}, {Country: Spain}, {Release Date: Nov 11, 2005}, {Comments: Box and docs are in Spanish; game is in English.}, {Country: Italy}, {Release Date: Nov 11, 2005}, {Comments: Box and Docs are in Italian; game is in English.}"

所以,我想把字符串拆分成这样(6个元素):

  • {发布人:THQ Inc.},{开发商:Heavy Iron Studios,Inc.},{国家/地区:美国},{发布日期:2005年11月1日},{UPC-A:7 xa 085138 xa 038046 xa 09},{任天堂媒体产品编号:美国
  • {国家/地区:法国},{发布日期:2005年11月11日},{EAN-13:第一章
  • {国家/地区:英国},{发布日期:2005年11月11日}
  • {国家:德国},{发布日期:2005年11月11日}
  • {国家/地区:西班牙},{发布日期:2005年11月11日},{备注:框和文档为西班牙语;游戏是英语的。}
  • {国家/地区:意大利},{发布日期:2005年11月11日},{备注:Box和Docs为意大利语;游戏是英语的。}

我尝试使用这个正则表达式代码,但它会将我预期的第一个元素拆分为两个,从而产生7个元素。(,\s)(?:\{Countr.*?)

vi4fp9gy

vi4fp9gy1#

对于regex“测试”,我建议使用一个好的website
这个正则表达式应该可以:result = re.split(r',(?![^{]*\})', s)
当使用字符串s:

s = "{Published by: THQ Inc.}, {Developed by: Heavy Iron Studios, Inc.}, {Country: United States}, {Release Date: Nov 01, 2005}, {UPC-A: 7xa085138xa038046xa09}, {Nintendo Media PN: DL-DOL-GIQE-USA}, {Country: France}, {Release Date: Nov 11, 2005}, {EAN-13: 4xa0005209xa0068024}, {Country: United Kingdom}, {Release Date: Nov 11, 2005}, {Country: Germany}, {Release Date: Nov 11, 2005}, {Country: Spain}, {Release Date: Nov 11, 2005}, {Comments: Box and docs are in Spanish; game is in English.}, {Country: Italy}, {Release Date: Nov 11, 2005}, {Comments: Box and Docs are in Italian; game is in English.}"

您将获得六个输出字符串:

{Published by: THQ Inc.}, {Developed by: Heavy Iron Studios, Inc.}
{Country: United States}, {Release Date: Nov 01, 2005}, {UPC-A: 7xa085138xa038046xa09}, {Nintendo Media PN: DL-DOL-GIQE-USA}"
{Country: France}, {Release Date: Nov 11, 2005}, {EAN-13: 4xa0005209xa0068024}", "{Country: United Kingdom}, {Release Date: Nov 11, 2005}
{Country: Germany}, {Release Date: Nov 11, 2005}
{Country: Spain}, {Release Date: Nov 11, 2005}, {Comments: Box and docs are in Spanish; game is in English.}
{Country: Italy}, {Release Date: Nov 11, 2005}, {Comments: Box and Docs are in Italian; game is in English.}
eqfvzcg8

eqfvzcg82#

不需要使用正则表达式。
我用一句话解决了你的问题:

['{Coun'+i if 'try:' in i else i for i in your_raw_string.split(', {Coun')]

相关问题