Regular Expressions

Regular expressions in Nushell’s commands are handled by the rust-lang/regex crate. If you want to know more, check the crate documentation: “regex”.

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.

Tips

Intro

str replace -r uses a regular expression.

"ab" | str replace -r "b" "c"

parse -r captures a pattern into columns.

"abc123" | parse -r '(?P<n>\d+)'

Basic

Read the digits a pattern captured.

"a1" | parse -r '(?P<d>\d)' | get d

Replace a pattern, then measure the text.

"red" | str replace -r "re" "go" | str length

Intermediate

Count the captures from a pattern.

"a1b2" | parse -r '(?P<d>\d)' | get d | length

Replace a digit in each string.

["a1" "b2"] | each {|s| $s | str replace -r '\d' "n"} | length

Advanced

Capture digits and measure the text.

"abc123" | parse -r '(?P<n>\d+)' | get n | first | str length