W3cubDocs

/Nushell

parse for strings

Parse columns from string data using a simple pattern.

Signature

> parse {flags} (pattern)

Flags

  • --regex, -r: use full regex syntax for patterns

Parameters

  • pattern: the pattern to match. Eg) "{foo}: {bar}"

Input/output types:

input output
list<any> table
string table

Examples

Parse a string into two named columns

>"hi there"|parse"{foo} {bar}"
╭───┬─────┬───────╮
# │ foo │  bar  │
├───┼─────┼───────┤
0hithere
╰───┴─────┴───────╯

Parse a string using regex pattern

>"hi there"|parse-r'(?P<foo>\w+) (?P<bar>\w+)'
╭───┬─────┬───────╮
# │ foo │  bar  │
├───┼─────┼───────┤
0hithere
╰───┴─────┴───────╯

Parse a string using fancy-regex named capture group pattern

>"foo bar."|parse-r'\s*(?<name>\w+)(?=\.)'
╭───┬──────╮
# │ name │
├───┼──────┤
0bar
╰───┴──────╯

Parse a string using fancy-regex capture group pattern

>"foo! bar."|parse-r'(\w+)(?=\.)|(\w+)(?=!)'
╭───┬──────────┬──────────╮
# │ capture0 │ capture1 │
├───┼──────────┼──────────┤
0foo
1bar
╰───┴──────────┴──────────╯

Parse a string using fancy-regex look behind pattern

>" @another(foo bar)   "|parse-r'\s*(?<=[() ])(@\w+)(\([^)]*\))?\s*'
╭───┬──────────┬───────────╮
# │ capture0 │ capture1  │
├───┼──────────┼───────────┤
0@another (foobar) 
╰───┴──────────┴───────────╯

Parse a string using fancy-regex look ahead atomic group pattern

>"abcd"|parse-r'^a(bc(?=d)|b)cd$'
╭───┬──────────╮
# │ capture0 │
├───┼──────────┤
0b
╰───┴──────────╯

Copyright © 2019–2023 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/parse.html