Question

I want to know how to match a string in a specified region: for example, I hava a C++ code snippet:

/*
    This is .... bla bla bla...int, float,...
    @author: Tom
*/
int a;
float b;

then I want to find out the keywords like int,floatoutside the comment, how to? I was thinking if I could use pre-search. But a simple sample, \b\w+(?<=er)\b matches words like 'worker','super',how to do if I only want the sub string of these results before er.And another example, if I only need to match the line comment in c++ code, if I use //.*?\n,the \nis also in the result, but I don't need it... update: I don't know if I have explained my problem clearly, and i don't know how to give a title to my question... :(

Was it helpful?

Solution

You really shouldn't parse source code with regular expressions. Use the right tool for the job... Regular expressions aren't the right tool, because it is very hard to take the context into account. You already observed this problem yourself.

Having said this, generally, you can use groups in your regular expressions to get only part of a match.

For example, the regular expression a(b)c will match the string abc, but the result will contain two groups.

  1. The first group is the complete match abc.
  2. The second group is the first sub-group b.

The example to match the single line comment in C++ could look like this:

//(.*?)\n

The first sub-group would than contain only the comment text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top