+ 3
Excluding sequences in regex.
Hello! I wanted to make a program to remove multiline comments /* */ from a string using regex. Pattern simply starts with \*, but then it includes all the following sequences until 1 */, how can I write that in regex? I thought of r"/\*(Anything except */)*\*/", but how do I write 'Everything except */?
19 Antworten
+ 2
how about a lexer-like method ?
something like this ?
https://code.sololearn.com/c5z9Z41tFgPd/?ref=app
is it overkill ?
+ 6
Seb TheS Try this:
(\/\*(.|\s)*\*\/)
Here's a demo I created:
https://regex101.com/r/fGL7AW/1
+ 5
Seb TheS At this point you really should provide some sample data of what you're thinking of.
What do you mean by "more comments"?
Like... nested comments such as:
/*
Outer
/*
Nested
*/
*/
If so, where would that be valid?
Or do you mean back to back comments like:
/*
Comments
*/
/*
More comments
*/
That said, do you have any other feedback like...
"Nice... that works for X scenario. But it doesn't work for this other scenario. Thanks guys for the assist. 😉👌"
idk... responses like, "It will fail when there are more comments" sort of makes it feel like we're going to be chasing a moving target while blind folded with no end in sight.
It sort of sucks the wind out of wanting to assist. (Just saying 🤷♂️)
+ 3
Seb TheS i added regex solution to the code, one is modified version from David Carroll's answer
+ 2
For example [^aeiou] would match any character but aeiou. Maybe you can write something like [^*/].
+ 1
Aaron Eberhardt /* is still accepted inside a multiline comment, but [^*/]|(/*) would then work
+ 1
Aaron Eberhardt No, [^*/] would also ignore single *s and /s, that's not wanted.
+ 1
Maybe this:
([^*/]|\*/|\*|/)
+ 1
David Carroll It will fail when there are more comments.
+ 1
David Carroll I talked about the second case:
/*
*/
/*
*/
Sorry, thought it was obvious.
+ 1
Rei Can be, I know how to solve the problem without regex, but it seems a big mess to solve it with regex, and I thought regex was supposed to make text wrapping easy.
+ 1
its somehow similar with parsing html, people screaming at me to not use regex.
regex can get complicated really easily, much harder to look at and understand. at some cases making going back and work on it again in the future harder than it should.
+ 1
Rei It seems to work, I have no idea why it works, but I didn't find any mistake cases.
\/\*(.|\s)*?\*\/
+ 1
Rei I'm glad you were able to jump in. I'm just now seeing the responses and would have gone with using a non greedy (or lazy) quantifier as well.
Thanks for the assist. 😉👌
+ 1
The problem is solved, thanks for all.
0
may be
^\/\*+[^\*\/]*\*+\/$
0
its lazy quantifier, unlike greedy that take as much as it can take. lazy quantifier, try to end the match a soon as it can be.
0
Привет
0
Напиши мне маленькую программу и скинь мне по ссылке