Giter Club home page Giter Club logo

psstringscanner's Introduction

PowerShell String Scanner

Provides lexical scanning operations on a String.

Ported from https://github.com/ruby/strscan

Parsing a Config File

Usage

$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

$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

$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.

$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.

$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.

$scanner = New-PSStringScanner 'Fri Dec 12 1975 14:39'

$scanner.SkipUntil("12")   # 10
$scanner
# s                     pos
# -                     ---
# Fri Dec 12 1975 14:39  10

psstringscanner's People

Contributors

dfinke avatar irwins avatar briantist avatar iisresetme avatar

Stargazers

Tuan Duc Tran avatar DeloDev avatar  avatar  avatar  avatar Michael Schreiber avatar  avatar  avatar  avatar Matthew Gray avatar  avatar Bert Mueller avatar __ avatar PSingletary avatar Matthew avatar John Dansak avatar  avatar  avatar Peter Pisarczyk avatar Jason Archer avatar  avatar Francisco Navarro avatar  avatar Luke Leigh avatar Herculosh C0de avatar Kevin Marquette avatar Flavien MICHALECZEK avatar Nick Polyderopoulos avatar  avatar Manuel avatar Jason Tucker avatar Michael Kelley avatar James Gutierrez avatar Charlie Smith avatar Darrin Britton avatar Ritch Melton avatar  avatar Mike F. Robbins avatar Guido Oliveira avatar Zafer Balkan avatar Fraefel avatar John Bevan avatar François-Xavier Cat avatar sheldonhull avatar Andrew Pla avatar Warren Frame avatar  avatar Van Gulick avatar Gary Ewan Park avatar

Watchers

 avatar James Cloos avatar Van Gulick avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.