user3332404
user3332404

Reputation: 171

How to exclude string using Regex?

I have below data

May 18 22:15:04 172.24.11.74 "abc","5","0",........

I want to capture only abc it could be anything (abc,bcd, aaa or it can be empty ), so I want to ignore May 18 22:15:04 172.24.11.74 . Also after Immediate match it should stop .So in simple terms I want to match everything between first "". Please help me to understand how I can achieve it . I tried following

(?<=")([\w\s]+)(?=")

It works fine only if first double quotes have some value . But when it is blank it moves further and capture next value in the double quotes . For example for below message it captures 5.

May 18 22:15:04 172.24.11.74 "","5","0"

I hope I am clear with my requirement .

Upvotes: 1

Views: 75

Answers (1)

Fuzzzzel
Fuzzzzel

Reputation: 1773

I think you were almost there.

If you make the group in the middle optional by adding a ? behind it, it matches even if there's nothing between the double quotes:

(?<=")([\w\s]+)?(?=")

Alternatively you can match the middle part * times instead of + times which means one or more times and by this does not match if the part is empty:

(?<=")([\w\s]*)(?=")

Upvotes: 1

Related Questions