0

I want to match the first colon : of this string (in R):

Ireland: Emergency Service Medal: Defence Forces

How to achieve this?

I tried many options from various questions here on StackExchange, but they always point to both colons. For example:

:(?=.*)

or:

(?<=.+?):(?=.+)

The following matches everything up to the first colon; but I only want the first colon, nothing before and nothing after that:

^([^:]*?)\s*:\s*

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
anpami
  • 760
  • 5
  • 17
  • 4
    With a perl like pattern, you can use `^[^:]+\K:` – The fourth bird Mar 12 '21 at 10:51
  • 1
    @Thefourthbird, thank you - perfect solution. Do you want to post it as a full-fledged response so that I can 'accept' it? – anpami Mar 12 '21 at 10:54
  • What are you planning to do with the first colon? I doubt you want to extract it. Replace/remove? Then it is still the same as matching *everything up to the first colon*: replace `^([^:]*):` with `$1` – Wiktor Stribiżew Mar 12 '21 at 10:54
  • @WiktorStribiżew, I want to use it as a separator. – anpami Mar 12 '21 at 10:55
  • 1
    Then there is nothing easier, match and capture with `^([^:]*):(.*)` / `^([^:]*):([\s\S]*)` / `(?s)^([^:]*):(.*)`. See [Splitting string from the first occurrence of a character](https://stackoverflow.com/questions/6131195), [How to split a string on the first occurrence of one of multiple substrings in JavaScript?](https://stackoverflow.com/questions/7527374). No regex required in .NET code, see [Split string based on the first occurrence of the character](https://stackoverflow.com/questions/21519548) (although [you can](https://stackoverflow.com/questions/52178533))... – Wiktor Stribiżew Mar 12 '21 at 10:56
  • `^([^:]+):(.*)` you wil have $1 and $2 when you do a regex replace – Daantje Mar 12 '21 at 10:57
  • What is your environment, programming language? – Wiktor Stribiżew Mar 12 '21 at 11:00
  • @WiktorStribiżew, thank you for the three solutions - strangely, none of them worked for me with `separate()`in `R` - the columns that I want to separate into are just empty with your three solutions... But anyway, the solution by @Thefourthbird worked well (`^[^:]+\K:`). – anpami Mar 12 '21 at 11:06
  • Ok, in R, you need to use `extract`, not `separate`. – Wiktor Stribiżew Mar 12 '21 at 11:07
  • See https://stackoverflow.com/questions/58206257/separate-character-string-at-first-digit-with-in-the-string/58492154#58492154 – Wiktor Stribiżew Mar 12 '21 at 11:10

0 Answers0