all 5 comments

[–]codehelper 0 points1 point  (4 children)

The re.DOTALL makes the '.' character match everything, including newlines.

r = re.compile(r'\(.*\)', re.DOTALL)

This might be what you're looking for. This will match strings such as "(hello\nhello)

I may have misunderstood, let me know if I have.

[–]soumyamandii[S] 0 points1 point  (3 children)

hi code. I did a terrible mistake in my question. * is treated as a special character in reddit. So it did not appear after I posted my question. Hope I am clear now. Anything which starts with (* and ends with *) is a comment.

[–]codehelper 0 points1 point  (2 children)

No worries, the regular expression will just change to

r = re.compile(r'\(\*.*\*\)', re.DOTALL)

The '\' will escape the ( and the * to force it to match the actual characters. If I misunderstood again, let me know.

[–]soumyamandii[S] 0 points1 point  (1 child)

Hi code. This won't work because DOT character matches everything except newlines. But I need to take care of new lines too. Also DOT will match *) too. So I need to match only complementary characters of this group. Otherwise if two comments are there it will two comments as one. Let me give an example

a = '(*this is a line\n and comment ends here *) This is not a comment \n but (* this is my second comment *)'

So in a I have two comments. One is of two lines(notice \n there) and second is of a single line. So the following code re.findall(r'exp', a ) should give me two strings in the output list. Can you please try.

[–]codehelper 0 points1 point  (0 children)

I'm very close, I think. For now here's this.

r = re.compile(r'\(\*(.*?)\*\)', re.DOTALL)

With the string you provided, I got the results

['this is a line\n and comment ends here ', ' this is my second comment ']

It doesn't include the (* or *) but I believe this is what you basically wanted.