Regex advice needed - C language

0 views
Skip to first unread message

Shreyas

unread,
Oct 14, 2009, 11:28:08 PM10/14/09
to Regex
How do I construct a regex that finds just the first instance in the
following test sample -

#define LOGGING_ENABLED
//#define LOGGING_ENABLED
// #define LOGGING_ENABLED
/* #define LOGGING_ENABLED */
/*#define LOGGING_ENABLED*/

i.e. the result should be #define LOGGING_ENABLED. As you can see I am
trying to find instances of the #define which are not commented out.

I'd appreciate any help.

- Shreyas.

eugeny....@gmail.com

unread,
Oct 16, 2009, 2:45:51 AM10/16/09
to Regex
try this

[\r\n]+\s*\#define\x20+LOGGING_ENABLED\s*[\r\n]+

that is line break(s) followed by optional space(s) followed by
"#define LOGGING_ENABLED" followed bt optional space(s) followed by
linebreak(s)

while this enhancement should fit for start of file and end of file
locations, as well.

(\A|[\r\n]+)\s*\#define\x20+LOGGING_ENABLED\s*([\r\n]+|\Z)


excerpt from help:
--------------------
\A only ever matches at the start of the string. Likewise, \Z only
ever matches at the end of the string. These two tokens never match at
line breaks. This is true in all regex flavors discussed in this
tutorial, even when you turn on "multiline mode". In EditPad Pro and
PowerGREP, where the caret and dollar always match at the start and
end of lines, \A and \Z only match at the start and the end of the
entire file.

JavaScript, POSIX and XML do not support \A and \Z. Use the caret and
dollar for this purpose.

Shreyas

unread,
Oct 20, 2009, 6:39:35 PM10/20/09
to Regex
Thank you, that worked like a charm!

Note to self -
Learn RegEx properly! :D

On Oct 16, 7:45 pm, "Eugeny.Satt...@gmail.com"
Reply all
Reply to author
Forward
0 new messages