D.Ng
D.Ng

Reputation: 57

Regex parse by commas

I am trying to parse the log file below; however I am a bit stuck on figuring out how to parse it by commas.

NOTICE Failed-Attempt: EAP session timed out, ACSVersion=acs-1.6.0.10-B.153.x86_64, ConfigVersionId=100, UserName=username, NAS-IP-Address=10.10.10.10, Calling-Station-ID=0123.4a56.78b9, NAS-Port-Id=123, AcsSessionID=host/123/321, AuthenticationIdentityStore=AD1, AuthenticationMethod=AuthMethod, SelectedAccessService=Wireless, DetailedInfo=Invalid username or password specified\, Retry is  allowed, FailureReason=24421 

I tried the following parse method, however it is not returning my intended results: (?:[^,]+)

The ideal goal is to match the following:

NOTICE Failed-Attempt: EAP session timed out
ACSVersion=acs-1.6.0.10-B.153.x86_64
UserName=username
NAS-IP-Address=10.10.10.10
Calling-Station-ID=0123.4a56.78b9
NAS-Port-Id=123
AcsSessionID=host/123/321
AuthenticationIdentityStore=AD1
AuthenticationMethod=AuthMethod
SelectedAccessService=Wireless
DetailedInfo=Invalid username or password specified, Retry is allowed
FailureReason=24421

Upvotes: 1

Views: 44

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You may match any 1+ chars other than a comma and backslash, or any escape sequence using

/(?:[^\\,]|\\.)+/s

In PHP:

$regex = '~(?:[^\\,]|\\.)+~s';

See the regex demo.

Details

  • (?: - start of a non-capturing group:
    • [^\\,] - any char other than \ and a comma
    • | - or
    • \\. - a \ followed with any char including line break chars (due to the s modifier)
  • )+ - end of the group, repeat 1 or more times.

A more efficient version of the regex (assuming the match should start with a char other than whitespace and a comma):

/(?=[^,\s])[^\\,]*(?:\\.[^\\,]*)*/s

In PHP:

$regex = '/(?=[^,\s])[^\\\\,]*(?:\\\\.[^\\\\,]*)*/s';

See this regex demo

The (?=[^,\s]) positive lookahead requires the char to the right to be any char but a , and whitespace, and [^\\,]*(?:\\.[^\\,]*)* is the unrolled equivalent of the regex explained above.

Upvotes: 1

Related Questions