Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Python re.sub() how to include and slice the match in the substitution?

Parent

Python re.sub() how to include and slice the match in the substitution?

+3
−0

I recently started learning regular expressions and I'm trying to use Python's re module, specifically the re.sub() function, to convert a subset of Markdown syntax to HTML whenever it appears in a string. However, I haven't been able to figure out how to slice the source string so that the Markdown syntax is removed.

For example, the string This is a *test* string. should get converted to This is a <i>test</i> string., but it keeps the asteriks like so: This is a <i>*test*</i> string.

This is my code (the regex checks for backslashes in case the syntax is escaped and is non-greedy in case of multiple matches):

testString = re.sub(r'(?<!\\)\*.*?\*(?!\\)', r'<i>\g<0></i>', testString)

I've tried splitting up the substitution and using string splicing like this r'<i>' + r'\g<0>'[1:-1] + r'</i>', but that just returns an italicized 'g'.

History

0 comment threads

Post
+2
−0

To answer your immediate question, use a capture group.

re.sub(r'(?<!\\)\*(.*?)\*(?!\\)', r'<i>\g<1></i>', testString)

With that out of the way, text parsing for a language is probably best served by an existing library.

For instance, r'(?<!\\)\*(.*?)\*(?!\\)'
is probably meant to be r'(?<!\\)\*(.*?)(?!\\)\*',
but r'This is a *test\* string*' will still mess up what you think it should do.

History

1 comment thread

Thanks for the suggestion. It works perfectly. My intention was more to learn how to use re.sub() in ... (2 comments)
Thanks for the suggestion. It works perfectly. My intention was more to learn how to use re.sub() in ...
pycoder‭ wrote 11 months ago

Thanks for the suggestion. It works perfectly. My intention was more to learn how to use re.sub() in this manner, which is why I didn't use a library.

Michael‭ wrote 11 months ago

Yeah, no worries. Groups are really helpful for lots of things! Learning regex is really worthwhile!

I just wanted to caution you that if this is supposed to be part of a generic Markdown tokenizer, you will eventually run into gnarly situations.