0

I'm trying to create a regex query to extract everything between the characters

^1234: 

and

, ^5678:

I tried the following and it doesn't work. Am i on the right track?

^(\^1234),(\^5678)$

Example text:

54:16812344266, ^1234:iwantthistext, ^5678:idontwantthis, ^0000: idontwantthiseither
  • Depends on the tool you're using too. You likely want a capture group to pick the part in between those two markers. In the regex you have, nothing is allowed in between (well except the comma, if we count that) – ilkkachu Jun 09 '23 at 06:31

2 Answers2

0

If you're using sed, you can do something like this:

$ echo '54:16812344266, ^1234:iwantthistext, ^5678:idontwantthis, ^0000: idontwantthiseither' | sed 's/.*\^1234:\(.*\), \^5678.*/\1/'
iwantthistext

You want to use the () to capture the match in between the strings. Then replace the full value with the capture text.

The first .* will match everything before ^1234 and the second .* will match everything after ^5678. The ^ needs escaping with a backslash so sed doesn't use it for interpolation.

Foo L
  • 10,977
  • 8
  • 40
  • 52
0

Like this: \^1234(.*?), \^5678

With input 54:16812344266, ^1234:iwantthistext, ^5678:idontwantthis, ^0000: idontwantthiseithere will output:

https://regex101.com/r/22Fplx/1

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40