Zackary Dane
Zackary Dane

Reputation: 11

Remove everything after second colon

I need to remove everything after the colon following orange

Example:

apple:orange:banana:grapes:

Becomes:

apple:orange

I've looked up a million different references for this and cannot find a solution.

Currently doing this in Notepad++ using the Find/Replace function.

Upvotes: 1

Views: 3413

Answers (3)

Joseph
Joseph

Reputation: 41

If I understand you correctly, you can use the plugin ConyEdit to do this. You can use its command cc.dac <nth>/<regex>/[<mode>] [-options]. cc.dac means: delete after column.

For Example:
With ConyEdit running in the background, copy the text and the command line below, then paste:

 apple:orange:banana:grapes:
 cc.dac 2/:/ -d

Upvotes: 1

bhavneet
bhavneet

Reputation: 61

How big is the file?

If it's a small file, you could probably write a simple code something like following snippet in java. Most the programming languages would support such operations.

String input = "apple:orange:banana:grapes:";
String[] arrOfStr = input.split(":");
int index = arrOfStr.indexOf("orange");
String[] arrOfStrSub = Arrays.copyOf(arrOfStr, 0, index);
String output = StringUtils.join(arrOfStrSub, ':');

Upvotes: 0

Paolo
Paolo

Reputation: 26240

Find what : (^[a-z]+:[a-z]+).*$

  • (^[a-z]+:[a-z]+) First capturing group. Match alphabetic characters at start of string, a colon, alphabetic characters.
  • .*$ Match anything up to the end of the string.

Replace with : \1

  • \1 Replace with captured group one.

You could of course make the expression more general:

Find what : (^[^:]+:[^:]+).*$

  • (^[^:]+:[^:]+) Capturing group. Match anything other than a colon at start of string, a colon, anything other than a colon.
  • .*$ Match anything up to end of string.

Replace with : \1

  • \1 Replace with captured group one.

As pointed out by revo in the comment below, you should disable the matches newline option when using the patterns above in Notepad++.

Upvotes: 2

Related Questions