PHP Developers Network
http://forums.devnetwork.net/

How do I skip lines that start with #
http://forums.devnetwork.net/viewtopic.php?f=38&t=141555
Page 1 of 1

Author:  notcoder [ Tue Jul 14, 2015 10:26 pm ]
Post subject:  How do I skip lines that start with #

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 [url]regexpal.com[/url]tool that I'm using for testing. Neither does regex101.com tool so I think the expression needs work.

Any tips would be appreciated.

Author:  requinix [ Tue Jul 14, 2015 10:51 pm ]
Post subject:  Re: How do I skip lines that start with #


Author:  notcoder [ Wed Jul 15, 2015 9:07 am ]
Post subject:  Re: How do I skip lines that start with #

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

Author:  notcoder [ Thu Jul 16, 2015 8:23 pm ]
Post subject:  Re: How do I skip lines that start with #

Anything.... please

Author:  Celauran [ Thu Jul 16, 2015 8:55 pm ]
Post subject:  Re: How do I skip lines that start with #

The pattern requinix provided is fine.

Syntax: [ Download ] [ Hide ]
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 >

Author:  requinix [ Thu Jul 16, 2015 8:57 pm ]
Post subject:  Re: How do I skip lines that start with #

Is there a "multiline" mode thing available? If so it probably needs to be enabled.

Author:  notcoder [ Thu Jul 16, 2015 10:20 pm ]
Post subject:  Re: How do I skip lines that start with #

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.

Author:  requinix [ Thu Jul 16, 2015 11:03 pm ]
Post subject:  Re: How do I skip lines that start with #

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.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/