简体   繁体   中英

Regex to Find values with quotes,Semicolon and Equals to

My Query is simple and my concern is very simple and very new to REGEX. I'm trying to match the records with Double quotes .

Example :

This is my sample Record :

"eventType":"delete","ServerSerial":"1556562030","ServerName":"XYZ_U_O","deletedat":"2018-08-24 17:56:39.974"}


{"eventType":"delete","ServerSerial":"0","ServerName":"","deletedat":"2018-08-24 17:56:34.944"}

First record is valid record with Serverserial and Server Name, But the second record is invalid with 0 as server serial and empty ServerName.

I want to match the second record to eliminate the records . I did try this it is matching both the records.

**^(?=.*?\bdelete\b)(?=.*?\bServerName\b).*$** 

With my expertise I couldnt do it . Can anyone help me to resolve this huddle

This will match any line that has a server name of "" or a server serial of 0

.*(?<=ServerSerial":")0(?=").*|.*(?<=ServerName":")(?=").*

Demo

To add this as a full answer..

  • Just search for the fixed part of the string in question and use it as front-anchor,
  • then match till the first curly bracket.
  • As requested, I've added an optional comma (if case the undesired data shows up at the end)
{"eventType":"delete","ServerSerial":"0","ServerName":""[^}]+},?

Regex Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM