Match Everything Until And Everything After With Regex Lookaheads And Lookbehind

  1. lookahead
  2. lookbehind

1. regex: lookaheads and lookbehinds

  • concepts to define patterns that only match they they
  • are / are not followed by a certain pattern → lookahead (positive / negative)
  • are / are not preceded by a certain pattern → lookbehind (positive / negative)
name       | pattern
-----------|-------------------------------------
lookahead | MATCH_PATTERN(?=LOOKAHEAD_PATTERN)
lookbehind | (?<=LOOKBEHIND_MATTERN)MATCH_PATTERN

2. example: lookbehind with the lookup of a bucket from the HashiCorp Consul CV

  • if I want to capture name that is after mr + an empty string (whitespace \s)
  • i need to use lookbehind syntax
[regex]::matches(‘mr paul’,’(?<=mr\s).*’).value
→→→ paul
  • I am getting the URL of the s3 bucket that is preceded by aws_setup_prod1/output/s3_bucket_kinesis_s3_enriched_id: with Hashicorp Consul in this way (and copy to clipboard for efficiency)

$regex = "(?<=aws_setup_prod1/output/s3_bucket_kinesis_s3_enriched_id:).*"
$returnedString = consul kv get -recurse customer/$consulName |
Select-String -pattern $regex
$streamingBucket = $returnedString.matches.value
$streamingBucket | clip
write-host $streamingBucket pasted to clipboard -foregroundcolor cyan

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

NVM Install Apple macOS M1 Silicon Chip

[iOS]Expense Tracker. Part 1. UI design

SwiftUI Tutorial: Using Button Role

SwiftUI Tutorial: Working with Images

Should I Learn SwiftUI instead of UIKit?

Send an email within the App | Xcode 12, Swift 5.3

The app mail display on an iPhone’s screen

Making Async Url Calls in Swift 5.5

ToDo, FixMe, Warning & Error | Code Documentation | Xcode Tips | Part 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

More from Medium

Drawing Heart using Python

The Parameters And Usage Of Ranges In Python

Django under the hood — Login

Python code formatter Black