0

Is there a way to match the zero-length position immediately following a known character (akin to matching essentially an insertion point that follows it?)

Consider this source text. In this example, I'm trying to match the zero-length positions immediately following any colons.

foo(laa:hee:)

Here's the same string but with pipe characters representing the locations I'm trying to match.

foo(laa:|hee:|)

The plan is to replace (or insert) values immediately after the colon, hence me wanting to match zero-length since everything else gets shifted to the right.

As an example, here's the same thing after the replacements are done. Here I've inserted '123' in the first "insertion" point, and '456' in the second.

Before: foo(laa:hee:)
After:  foo(laa:123hee:456)

Since I don't know how to match nothing (i.e. zero-length), my thought is since I know there will never be whitespace characters, to match zero-or-more whitespace characters that immediately follow a colon. I think I'm supposed to use a look-behind or something but I'm not really sure how to structure this, or even if this is the correct approach.

Here's more examples followed by the same but with pipes where I'd like to match:

Original     Desired Result
test(:)      test(:|)       <-- One match
test(foo:)   test(foo:|)    <-- One match
test(_:)     test(_:|)      <-- One match
test(::)     test(:|:|)     <-- Two matches
test(:laa:)  test(:|laa:|)  <-- Two matches
test()       test()         <-- No matches

If it matters, this is for Swift running on macOS.

Feedback on possible duplicate

I don't think this is a duplicate of the referenced question because that question talks about finding a specific string based on lookahead or lookbehind.

In my case I'm not looking for a string, but rather a position in a string (i.e. something that has zero-length), hence asking about the search pattern.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • Yes, use a positive Lookbehind: `(?<=:)`. Demo: https://regex101.com/r/JIUNsf/1 – 41686d6564 stands w. Palestine Aug 11 '22 at 00:50
  • 1
    Why not just replace `:` with `:123` – bobble bubble Aug 11 '22 at 01:16
  • @bobblebubble because as I've shown, there can be several colons in the string. That's why I'm trying to use capture groups. If there are six colons, I want six capture groups so I can replace them as needed. – Mark A. Donohoe Aug 12 '22 at 17:53
  • @41686d6564standsw.Palestine... perfect!! I can't believe it's that simple. lol Want to put that in an answer so I can accept it? – Mark A. Donohoe Aug 12 '22 at 17:55
  • @Mark Glad to help :) The question is closed as duplicate because it's already answered in the linked post. Feel free to upvote any answers you find useful there. Note that you can still do it without a Lookbehind as suggested by bobble above, regardless of how many colons the string has. You might have a specific case where a Lookbehind is the only option, but that wasn't explained in the question, AFAICT. – 41686d6564 stands w. Palestine Aug 12 '22 at 19:04
  • I've voted to have this reopened because that other question AFAIS didn't address matching zero-length, which is what I was after. Also, not sure how bobble's technique works as if you replace ':' with ':123' then want to replace the second ':' with ':456' the first ':' is still there. Of note, I've actually switched my implementation to scan across the string looking for what I need, and accumulating into a new string (is that what 'bobble' meant?) so this is more an exercise in understanding regex. – Mark A. Donohoe Aug 13 '22 at 10:02

0 Answers0