BobbyP
BobbyP

Reputation: 2265

Regular Expression to match string which doesn't contain substring

I have a comma separated list as shown below. The list is actually on one line, but I have split it up to demonstrate the syntax and that each single unit contains 5 elements. There is no comma at the end of the list

ro:2581,1309531682152,A,Place,Page,
me:2642,1310989368864,A,Place,Page,
uk:2556,1309267095061,A,Place,Page,
me:2642,1310989380238,D,Place,Page,
me:2642,1334659643627,D,Place,Page,
ro:3562,1378721526696,A,Place,Page,
uk:1319,1309337246675,D,Place,Page,
ro:2581,1379500694666,D,Place,Page,
uk:1319,1309337246675,A,Place,Page

What I am trying to do is remove any unit (full line) that does not begin with uk:. I.e., the results will be:

uk:2556,1309267095061,A,Place,Page,
uk:1319,1309337246675,D,Place,Page,
uk:1319,1309337246675,A,Place,Page

If the string was on separate lines as my example, I could do this relatively easy, but because it is all on one line, I cannot get it to work. Can anyone point me in the right direction?

Thanks

Upvotes: 1

Views: 87

Answers (2)

anubhava
anubhava

Reputation: 786291

I would suggest a simple regex like this:

(\buk:.+?,Page)(?:,|$)

and grab matched group #1

RegEx Demo

Upvotes: 0

RevanProdigalKnight
RevanProdigalKnight

Reputation: 1326

This should work:

(uk:\d+,\d+,\w,\w+,\w+)

Demo

It looks for uk: and then it's pretty much comma-counting from there on.

EDIT:

Since OP has now clarified that what they're using can only remove strings:

,?[^u][^k]:\d+,\d+,\w,\w+,\w+

Demo 2

This looks for an optional comma followed by two letters that are not u and not k in that order, then a colon (:), and then the rest of the regex is the same.

Upvotes: 3

Related Questions