Excluding sequences in regex. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 */?

24th Aug 2020, 8:06 AM
Seb TheS
Seb TheS - avatar
19 Answers
+ 2
how about a lexer-like method ? something like this ? https://code.sololearn.com/c5z9Z41tFgPd/?ref=app is it overkill ?
24th Aug 2020, 9:57 AM
Rei
Rei - avatar
+ 6
Seb TheS Try this: (\/\*(.|\s)*\*\/) Here's a demo I created: https://regex101.com/r/fGL7AW/1
24th Aug 2020, 8:45 AM
David Carroll
David Carroll - avatar
+ 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 🤷‍♂️)
24th Aug 2020, 9:41 AM
David Carroll
David Carroll - avatar
+ 3
Seb TheS i added regex solution to the code, one is modified version from David Carroll's answer
24th Aug 2020, 10:56 AM
Rei
Rei - avatar
+ 2
For example [^aeiou] would match any character but aeiou. Maybe you can write something like [^*/].
24th Aug 2020, 8:11 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
Aaron Eberhardt /* is still accepted inside a multiline comment, but [^*/]|(/*) would then work
24th Aug 2020, 8:13 AM
Seb TheS
Seb TheS - avatar
+ 1
Aaron Eberhardt No, [^*/] would also ignore single *s and /s, that's not wanted.
24th Aug 2020, 8:18 AM
Seb TheS
Seb TheS - avatar
+ 1
Maybe this: ([^*/]|\*/|\*|/)
24th Aug 2020, 8:26 AM
Seb TheS
Seb TheS - avatar
+ 1
David Carroll It will fail when there are more comments.
24th Aug 2020, 9:17 AM
Seb TheS
Seb TheS - avatar
+ 1
David Carroll I talked about the second case: /* */ /* */ Sorry, thought it was obvious.
24th Aug 2020, 9:46 AM
Seb TheS
Seb TheS - avatar
+ 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.
24th Aug 2020, 10:18 AM
Seb TheS
Seb TheS - avatar
+ 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.
24th Aug 2020, 10:28 AM
Rei
Rei - avatar
+ 1
Rei It seems to work, I have no idea why it works, but I didn't find any mistake cases. \/\*(.|\s)*?\*\/
24th Aug 2020, 11:25 AM
Seb TheS
Seb TheS - avatar
+ 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. 😉👌
24th Aug 2020, 4:09 PM
David Carroll
David Carroll - avatar
+ 1
The problem is solved, thanks for all.
25th Aug 2020, 2:19 PM
Seb TheS
Seb TheS - avatar
0
may be ^\/\*+[^\*\/]*\*+\/$
24th Aug 2020, 8:56 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
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.
24th Aug 2020, 11:30 AM
Rei
Rei - avatar
0
Привет
25th Aug 2020, 5:33 PM
artem
artem - avatar
0
Напиши мне маленькую программу и скинь мне по ссылке
25th Aug 2020, 5:34 PM
artem
artem - avatar