英文:
Capturing data in regex in python 3.11
问题
I've got the regex expression I want to use:
import re
user_input = '**Suggested by <@1234567890> (ID: 1234567890)**\n\ntesting new idea'
regex = re.compile(r'\*\*Suggested by <@(.*?)> \(ID: (.*?)\)\*\*\n+(.*$)')
content = re.search(regex, user_input)
print(content.group(1))
print(content.group(2))
print(content.group(3))
I'm getting this error:
I have tried double escaping (which as expected didn't work either)
The regex should work fine I think I might be using the wrong operation?
英文:
I've got the regex expression I want to use:
\*\*Suggested by \<\@(.*?)\> \(ID\: (.*?)\)\*\*\\n+(.*)$
along with a template string:
**Suggested by <@1234567890> (ID: 1234567890)**\n\ntesting new idea
This works when testing here: https://regex101.com - with a minor caveat (can't seem to disregard multiple new lines)
however it can be skipped by replacing \n+ with \n\n
When trying to use this in python:
import re
user_input = '**Suggested by <@1234567890> (ID: 1234567890)**\n\ntesting new idea'
regex = re.compile(r'\*\*Suggested by \<\@(.*?)\> \(ID\: (.*?)\)\*\*\\n+(.*$)')
content = re.search(regex, user_input)
print(content.group(1))
print(content.group(2))
print(content.group(3))
I'm getting this error
I have tried double escaping (which as expected didn't work either)
The regex should work fine I think I might be using the wrong operation?
答案1
得分: 0
Group the \\n, and then apply the + quantifier.
(?:\\n)+
\*\*由\<\@(.*?)\> \(ID\: (.*?)\)\*\*(?:\\n)+(.*)$
英文:
Group the \\n, and then apply the + quantifier.
(?:\\n)+
\*\*Suggested by \<\@(.*?)\> \(ID\: (.*?)\)\*\*(?:\\n)+(.*)$
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论