-6

How to get the string after 2 colons

:somelongstring::123abc:

how to get only the string 123abc

i tried this: Regex to find a string after the last colon

Community
  • 1
  • 1

2 Answers2

0

I'm pretty sure you just want this:

"::(\w+)"
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
  • What's the `@` and `"` for? And why are you escaping the `:`? – Maroun Nov 30 '16 at 20:04
  • `@` is used in c# anytime your using the `\\`` char in a string, and im escaping them because 1. its harmless and 2. there are certain scenarios in regex where `:` is special, like in `(?:` – maraaaaaaaa Nov 30 '16 at 20:06
  • **1.** You assume OP is using C#. **2.** There's no need at all to escape `:` here, why do you care about other cases that don't exist here? – Maroun Nov 30 '16 at 20:07
  • like i said, its harmless. Also OP never stated any language. – maraaaaaaaa Nov 30 '16 at 20:07
  • But it's not a good practice. It's harmless to add `$$$$` at the end as well. – Maroun Nov 30 '16 at 20:08
0

If I understood you correctly you want to find anything that is after double colon "::" and before single colon ":", as it is in your example. You could use this regex:

regex = "::(.*):";

This basically tells: Give me everything that starts with "::" and ends with ":" but without those start and end characters.

  • What if the string is `:somelongstring::123abc:123abc:`? Do we want to match one `123abc` or both? (Your regex will match both.) – Joe C Nov 30 '16 at 22:07
  • @JoeC You are definitely right. My regex would match both (123abc:123abc), and that is because regex is 'greedy' and it will try to match as much text as possible. Well, this is now a problem of defining what exactly we want to match, but I get your point. – Nemanja Milosevic Dec 01 '16 at 17:30