Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dfinke/psstringscanner
Provides lexical scanning operations on a String
https://github.com/dfinke/psstringscanner
parser parsing powershell scanner string
Last synced: 9 days ago
JSON representation
Provides lexical scanning operations on a String
- Host: GitHub
- URL: https://github.com/dfinke/psstringscanner
- Owner: dfinke
- License: mit
- Created: 2019-03-24T22:54:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-06-29T22:39:05.000Z (over 5 years ago)
- Last Synced: 2024-10-12T13:06:40.642Z (25 days ago)
- Topics: parser, parsing, powershell, scanner, string
- Language: PowerShell
- Size: 658 KB
- Stars: 49
- Watchers: 4
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PowerShell String Scanner
Provides lexical scanning operations on a String.Ported from https://github.com/ruby/strscan
# Parsing a Config File
# Usage
```powershell
$scanner = New-PSStringScanner 'This is an example string'$scanner.EoS() # -> False
$scanner.Scan("\w+") # 'This'
$scanner.Scan("\s+") # ' '
$scanner.Scan("\w+") # 'is'
$scanner.EoS() # -> False
$scanner.Scan("\s+") # ' '
$scanner.Scan("\w+") # 'an'
$scanner.Scan("\s+") # ' '
$scanner.Scan("\w+") # 'example'
$scanner.Scan("\s+") # ' '
$scanner.Scan("\w+") # 'string'
$scanner.EoS() # -> True
```# More Uses
Two approaches, same results.## Using Scan, Check and Skip
```powershell
$scanner = New-PSStringScanner 'Eggs, cheese, onion, potato, peas'$actualItems = @()
while ($true) {
$actualItems += $scanner.scan("\w+")
if ($scanner.Check(',')) {
$scanner.Skip(',\s*')
}
else {
break
}
}
```## Using Do {} Until
```powershell
$scanner = New-PSStringScanner 'Eggs, cheese, onion, potato, peas'$actualItems = do {$scanner.scan("\w+")} until ($scanner.EoS())
```# ScanUntil
Scans the string until the pattern is matched. Returns the substring up to and including the end of the match, advancing the scan pointer to that location. If there is no match, null is returned.
```powershell
$scanner = New-PSStringScanner 'Fri Dec 12 1975 14:39'$scanner.ScanUntil("1") # "Fri Dec 1"
$scanner.ScanUntil("YYZ") # $null
```# CheckUntil
This returns the value that ScanUntil would return, without advancing the scan pointer. The match register is affected, though.
```powershell
$scanner = New-PSStringScanner 'Fri Dec 12 1975 14:39'$scanner.CheckUntil("12") # "Fri Dec 12"
$scanner.pos # 0
```# SkipUntil
Advances the scan pointer until pattern is matched and consumed. Returns the number of bytes advanced, or null if no match was found.Look ahead to match pattern, and advance the scan pointer to the end of the match. Return the number of characters advanced, or null if the match was unsuccessful.
It's similar to ScanUntil, but without returning the intervening string.
```powershell
$scanner = New-PSStringScanner 'Fri Dec 12 1975 14:39'$scanner.SkipUntil("12") # 10
$scanner
# s pos
# - ---
# Fri Dec 12 1975 14:39 10
```