How do I skip lines that start with #

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
notcoder
Forum Newbie
Posts: 4
Joined: Tue Jul 14, 2015 10:13 pm

How do I skip lines that start with #

Post by notcoder »

I have a config file that I need to parse out. I am trying to write a very simple expression to match the variable on each line that does NOT start with a # (comment) character.

Here is a sample text from that config file:
# --------------------------
# some comments go here...
# -------------------------
#SSCOutputGenerationSchedule = 2015-07-03:21:00 5,2015-07-03:22:30,2015-07-04:14:00,2015-07-04:19:00
SSCOutputGenerationSchedule = 2015-08-05:14:00,2015-08-05:19:00,2015-08-10:15:30,2015-08-16:10:00

I want to match the SSCOutputGenerationSchedule variable on the last line and skip all lines that start with #. Here is what I've tried:

^(?!#)(\([A-Za-z]*\))

I found that expression on-line somewhere. It uses a negative look-back feature, which I thought would give me what I need, but it doesn't seem to work in the regexpal.comtool that I'm using for testing. Neither does regex101.com tool so I think the expression needs work.

Any tips would be appreciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I skip lines that start with #

Post by requinix »

That regex won't match your example because it's looking for parentheses...

But you don't have to care about the #s at all because you're looking for a [A-Za-z] variable.

Code: Select all

^[A-Za-z]+
notcoder
Forum Newbie
Posts: 4
Joined: Tue Jul 14, 2015 10:13 pm

Re: How do I skip lines that start with #

Post by notcoder »

Sorry, it still doesn't match anything - I tried that expression in two different tools this morning. It looks like it should work at least partially. Even if it did work, it would also be matching words in the comments section above, which is undesirable.

Therefore, I really need to find a way to skip lines all-together that start with #:

Examine first char at beginning of each line, if not #, then proceed to match the words in that line (alphanumerics and underscores). This solution comes close.... but still need to make it skip the whole line with # in pos 1:

(?!#)[A-Za-z]*

Produces this result:
#SSCOpututGenerationSchedule = 2015-07-03:21:00 ,2015-07-03:22:30,2015-07-04:14:00
SSCOpututGenerationSchedule = 2015-07-05:14:00,2015-07-05:19:00,2015-07-10:15:30
notcoder
Forum Newbie
Posts: 4
Joined: Tue Jul 14, 2015 10:13 pm

Re: How do I skip lines that start with #

Post by notcoder »

Anything.... please
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I skip lines that start with #

Post by Celauran »

The pattern requinix provided is fine.

[text]php > $s1 = '#SSCOpututGenerationSchedule = 2015-07-03:21:00 ,2015-07-03:22:30,2015-07-04:14:00';
php > $s2 = 'SSCOpututGenerationSchedule = 2015-07-05:14:00,2015-07-05:19:00,2015-07-10:15:30';
php > $pattern = '/^[A-Za-z]+/';
php > var_dump(preg_match($pattern, $s1));
int(0)
php > var_dump(preg_match($pattern, $s2));
int(1)
php >[/text]
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I skip lines that start with #

Post by requinix »

Is there a "multiline" mode thing available? If so it probably needs to be enabled.
notcoder
Forum Newbie
Posts: 4
Joined: Tue Jul 14, 2015 10:13 pm

Re: How do I skip lines that start with #

Post by notcoder »

Yes, one of the tools that I use does indeed have Multiline mode - I enabled it and it works! Thanks for that. Not sure what multi-line mode means but I'll look for it. THx again.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I skip lines that start with #

Post by requinix »

It means that ^ and $ will match at the beginning and end of lines, rather than just at the beginning and end of the entire string.
Post Reply