#πŸ”’ Regex Expression

17 messages Β· Page 1 of 1 (latest)

celest field
#

Hello there,
I am stuck on a regex problem;
tried looking into regex testers online.
It looks like it should work, but it doesn't? (burnt out lemon_pleading )

Anyways, any/all help would be appreciated y'all πŸ‘‹

Problem :
I am trying to replace certain parts
in a (multi-line) string that don't contain the sequence MK.

parts are separated by a string,
for now; lets say it's BBBB

Context :
"sample" text -

some random text

BBBB

abcdefg
*MK*
xyz

BBBB

more text

BBBB

*MK*
xyz

transforms into –

[Removed Text]

abcdefg
*MK*
xyz

[Removed Text]

*MK*
xyz

Attempt/s :
I've attempted using negative lookahead, but to no avail.

"B+(?!MK)[^B]+B*"

"(?!MK)B+.+?B*"

"B+[^B]+B*(?<!MK)"
ruby tigerBOT
#

@celest field

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fading path
#

Does it have to be regex? It doesn't seem too complicated without

true scaffold
#

you could split the string at BBBB and then check each string for MK

celest field
#

I've done a workaround, by splitting the text into chunks (BBBB being the separator); then looking if MK exists in each chunk

fading path
#

"".join(section for section in text.split("BBBB") if "*MK*" in section)

celest field
#

Reason being as for why i am looking more into regex - is because I had a previous version that utilized regex for similar simpler process.

celest field
zenith tapir
#

Yeahhh I would avoid using regex for that if you can

#

something like (^|BBBB)([\n\r\w ](?!MK))* might work for finding the places you want to remove, not sure how to invert it to keep them though

shrewd pilot
#

matching the places to remove is what they want, no? so they can just re.sub it
I agree with the others on not solely using regex, especially if this is gonna get modified again in the future

#

not that it's impossible to do it with regex, just unnecessary complexity

celest field
#

I think I'd just do the workaround and call it a day.
thanks again for all the help πŸ‘‹

#

!close

ruby tigerBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.