PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Apr 23, 2017 2:48 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Jul 14, 2015 10:26 pm 
Offline
Forum Newbie

Joined: Tue Jul 14, 2015 10:13 pm
Posts: 4
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.


Top
 Profile  
 
PostPosted: Tue Jul 14, 2015 10:51 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6385
Location: WA, USA
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:
^[A-Za-z]+


Top
 Profile  
 
PostPosted: Wed Jul 15, 2015 9:07 am 
Offline
Forum Newbie

Joined: Tue Jul 14, 2015 10:13 pm
Posts: 4
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


Top
 Profile  
 
PostPosted: Thu Jul 16, 2015 8:23 pm 
Offline
Forum Newbie

Joined: Tue Jul 14, 2015 10:13 pm
Posts: 4
Anything.... please


Top
 Profile  
 
PostPosted: Thu Jul 16, 2015 8:55 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6219
Location: Montreal, Canada
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 >

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Thu Jul 16, 2015 8:57 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6385
Location: WA, USA
Is there a "multiline" mode thing available? If so it probably needs to be enabled.


Top
 Profile  
 
PostPosted: Thu Jul 16, 2015 10:20 pm 
Offline
Forum Newbie

Joined: Tue Jul 14, 2015 10:13 pm
Posts: 4
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.


Top
 Profile  
 
PostPosted: Thu Jul 16, 2015 11:03 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6385
Location: WA, USA
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group