What Regex expression would be able to match colon-wrapped strings, and then the rest of the string segments, even if single colons are present?

Sample string input: “This is a :key: and this is :anotherKey: but this: is normal text.”

I’m attempting this Powershell, but was hoping to accomplish it all in the Regex expression and use the Matches method to return an array that I could loop through.

I can’t seem to figure out a Regex pattern that will return the following matches for the above string:

  1. This is a
  2. :key:
  3. and this is
  4. :anotherkey:
  5. but this: is normal text.

Below are the expressions I’ve tried and a description of their output

  1. (:S+:(?!S)|[^:]+)
    • The stand-alone colon is not matched
  2. (:S+:(?!S)|[^:]+|:)
    • The stand-alone colon is matched, but all on it’s own
  3. (:S+:(?!S)|[^:]+(:|$))
    • Each colon is matched on it’s own (this one is way off)
  4. [More attempts that only get worse]

2

The simplest regex would be :[^:]+:, which means match a colon followed by one or more not colons followed by a colon. However, that will allow spaces to be be included in the colon wrapped strings. If the colon wrapped string must not contain any whitespace, then perhaps use :[^:s]+:, which means match a colon followed by one or more not colons or whitespace followed by a colon. In either case, you do not need the negative lookahead.

If you want to get back a list of parts of the string, with the parts split by the colon wrapped parts, then you may need to use a split function in addition to regex. Unfortunately you didn’t indicate which language you are calling your regex from, so I will use Python, which I am most familiar with.

>>> import re
>>> s = "This is a :key: and this is :anotherKey: but this: is normal text."
>>> re.split(r'(:[^:]+:)', s)
['This is a ', ':key:', ' and this is ', ':anotherKey:', ' but this: is normal text.']

As you can see, the re.split function returned a list of the parts split on the matches to the colon wrapped strings. And because I wrapped the regex in parentheses, the resulting group matches are included in the output. You may need to consult the documentation for whichever language you are using for any slight differences in behavior.

1

I was able to approach the problem differently, thanks to the response from @Waylan.

I’m now using a far simpler expression to only match the colon-wrapped segments, and then splitting on those matches as a delimiter.

$inputString = "This is a :key: and this is :anotherkey: but this: is normal text."
$pattern = '(:[^:s]+:)'
$result = [regex]::Split($inputString, $pattern)
$result

Output

This is a 
:key:
 and this is 
:anotherkey:
 but this: is normal text

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật