Search code examples
regexregex-negationregex-lookaroundsregex-groupregex-greedy

RegEx for extracting chars between colons


I am trying to extract text between two colons, without them and spaces, but with my code I am getting two colons always.

Sentence is: xxxxxxx: hello world, my name is AAAA :description

Now, I would need: hello world, my name is AAAA

With (\:[^:]*\:) I am getting: : hello world, my name is AAAA :

How do I solve this problem?


Solution

  • You could use a lookarounds to assert what is on the left and what is on the right is a colon.

    For the matching you could use a negated character class [^:]* to match to match 0+ times not a colon. If you want to match at least 1 character you could change the quantifier to + instead of *

    (?<=:)[^:]*(?=:)
    

    Regex demo