英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论