Regular expressions in python

abhijangda

Padawan
hello everybody, I want to create a regular expression (in Python) for which I select all the string in between
/* and */ including all spaces, tabs, newline etc.
for example,
/* HELLO */
/* HELLO
EVERYONE */
these should be selected with and without \n. I have trying for 2 hrs and still trying. I was able to achieve without newline character, i.e. I can get string /* HELLO */ but not
/* HELLO
EVERYONE */
I did the following

/\*(\w+\s*+\n*)+\*/

/\*[\w+\s*+\n*]+\*/


any many other do not remember all that.
Please write that regular expression for me!!
 
i dont work on python but in C#, the entire text can be represented as a string with escape sequence
/t = tab
/n = new line

the string would look something like ::
/* HELLO/nEVERYONE */
and that works for the regex
 

QwertyManiac

Commander in Chief
I did the following

/\*(\w+\s*+\n*)+\*/

/\*[\w+\s*+\n*]+\*/

It is a simple matter of iterating over issues and building your solution.
Starting off, you will want to match 'any' string, not characters belonging to a set (A simple .* within comments, perhaps). Next, you'll need to handle detection of newline characters (\r as well as \n, if you may).

Then you'll need to understand that regex is greedy, since it would now be matching against multiple comments spread in a document (A /**//**/ would be matched as a single one). Once you've figured out a way to avoid THAT, you'll need to find a way to support comments that have multiple '*'s in them, a common sight for multi-line comments from IDEs/code-editors.

By now it would be a complex regex that STILL would not do it all for you (multiple ending *s in a line? boom!). But anyways, what are you gonna use it for?

Please write that regular expression for me!!

We could write it down for you, but that'd defeat the purpose of this board wouldn't it? But well, you can always search the web - your problem isn't unique.
 
Top Bottom