Baklap4
Baklap4

Reputation: 4202

Regex keep all lines with delimiter (sublime)

I'm having a file with lots of text but on some rows there are lines with just:

sometext:someothertext

What i want is to keep those lines. I've written a regex but that does the exact opposite of what i want:

[0-9a-zA-Z]+(:[0-9a-zA-Z]+)

Also there are a few lines with have the delimiter in it. I don't need to keep those. A sample line would be:

This is a very nice line saying stuff about sometext:someothertext

How would the regex look like to get just the lines containing sometext:someothertext format?

Upvotes: 1

Views: 139

Answers (2)

Michael Back
Michael Back

Reputation: 1871

Here is a negative example:

[^: \t]:[^: \t]

Match:

  1. not colon, space or tab
  2. colon
  3. not colon, space or tab

You can add to the "not" character class(es) anything else you would like to exclude.

Here is a more positive example:

[0-9A-Za-z]:[0-9A-Za-z]

Use with grep, awk, or sed and the above will match the line for printing:

grep '[^: \t]:[^: \t]' foo.txt
awk '/[^: \t]:[^: \t]/' foo.txt
sed '/[^: \t]:[^: \t]/ p; d' foo.txt

grep '[0-9A-Za-z]:[0-9A-Za-z]' foo.txt
awk '/[0-9A-Za-z]:[0-9A-Za-z]/' foo.txt
sed '/[0-9A-Za-z]:[0-9A-Za-z]/ p; d' foo.txt

If you want to match the whole line... You can use .* before and after either regex.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

You may use

^(?![0-9a-zA-Z]+:[0-9a-zA-Z]+$).*$\R*

This expression will work like this.

Replacement text is empty.

Pattern details:

  • ^ - start of line (the m multiline option is default in SublimeText)
  • (?![0-9a-zA-Z]+:[0-9a-zA-Z]+$) - the line should not match the pattern: 1+ alphanumerics followed with a : and again 1 or more alphanumerics
  • .*$ - matches the whole (even empty) line
  • \R* - and zero or more linebreaks.

If I have the following text:

local06:local16
TEXT local76:local16
local06:local17
local06:local19

More TEXT local76:local678

The result is:

enter image description here

Upvotes: 3

Related Questions