两个已知标记之间的子串提取与问题标记

huangapple go评论83阅读模式
英文:

Substring between known two markers extraction with problem markers

问题

@miernic asked long ago how do you extract an arbitrary string which is located between two known markers in another string.

My problem is that the two markers include Regular Expression's meta characters. Specifically, I need to extract ABCD from the string ('ABCD',), parenthesis, single quote and comma, all included in the source string. The extracted string itself might include single and double quotes, dots, parenthesis, and white space. The makers are always (' and ',).

I tried to use r' strings and lots of escape characters and nothing works.

Pleeeease....

英文:

@miernic asked long ago how do you extract an arbitrary string which is located between two known markers in another string.

My problem is that the two markers include Regular Expression's meta characters. Specifically, I need to extract ABCD from the string ('ABCD',), parenthesis, single quote and comma, all included in the source string. The extracted string itself might include single and double quotes, dots, parenthesis, and white space. The makers are always (' and ',).

I tried to use r' strings and lots of escape characters and nothing works.

Pleeeease....

答案1

得分: 2

将我的评论转为答案,以便将来的访问者能够轻松找到解决方案。

您可以使用这个正则表达式,以 " 作为正则表达式分隔符:

r"\('(.+?)',\)"

re.findall 中使用上述正则表达式,这样您就只会得到它捕获的组。

英文:

Converting my comment to answer so that solution is easy to find for future visitors.

You may use this regex with " as regex delimiter:

r"\('(.+?)',\)"

Use above regex in re.findall so that you get only captured group returned from it.

huangapple
  • 本文由 发表于 2020年1月3日 18:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定