4

I need to get the string in between /* */ comment tag. I am trying to parse a file and just get the strings between comment tags within it. I'm new in using regex. So any advise would be greatly appreciated. I already tried some but none are working.

Sample String Would Be. And yes there is a newline after /* and before */

/*
 Comment Here
 Comment Here
*/
1
  • I'm using PHP. Sorry I forgot to mention it ..:) Commented Nov 26, 2011 at 12:48

4 Answers 4

4

You haven't said what language you are using, but this regex will work for java (which doesn't require escaping of /):

(?s)/\*(.*?)\*/

Group 1 (ie \1) has the content you seek

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Bohemian .. I'm using PHP. Sorry I forgot to mention it.
2

It's tricky if you use / as the delimiter because then the slashes inside the regular expression will need escaping, so use a different delimiter instead:

"#/\*(.*?)\*/#s"

The s after the last delimiter is the DOTALL modifier.

Comments

2

Not sure what regex flavor you're looking for, but you can find a good discussion of finding comments using regular expressions here. Check it out -- it's not a simple as several of the early answers here suggest.

However, note that the regular expressions at the site matches comments -- you'll need to then apply one of the other answers here to extract the text as needed.

Comments

1

In php, use this

if (preg_match('%/\*(.*?)\*/%s', $subject, $matches)) {
    $result = $matches[1];
} else {
    $result = "";
}

You could also trim the match too if you wanted

$result = trim($matches[1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.