Skip to content

Wildcard Segments

William W. Kimball, Jr., MBA, MSIS edited this page Oct 11, 2020 · 18 revisions

There are three types of wildcard segments:

  1. Match every immediate child.
  2. Match every leaf node.
  3. A convenience shorthand for Array Element Searches and Hash Attribute Searches.

Match Every Immediate Child

This is a standalone * (single star) as a YAML Path segment. It looks like some.path.* or /some/path/*. More segments may follow it.

This is identical to a Search segment where the search term is [.!=""]. This translates to "match every Hash key for which its name is not empty and every Array element which is not empty". This operator also vertically expands results from Collectors, effectively breaking them out from an Array-per-line to one-Scalar-per-line. If you place this inside a Collector, the results will still be collected into an Array.

Match Every Non-Null Array Element and Every Non-Empty Hash Key

When you just want every non-empty immediate child:

---
array_values:
  - alpha
  - bravo
  - ''
  - 
  - charlie
  - delta

hash_structure:
  key1: value 1
  key2: value 2
  keyM: ''
  key3: value 3
  key4: value 4

Single star searches return:

  • array_values.* or /array_values/*:
alpha
bravo
charlie
delta

Notice that the empty-string element and the null element between "bravo" and "charlie" were both omitted. The * operator matches only non-empty, non-null elements from Arrays. When you want the empty-string element in the results, use the ** operator. However, while ** will return the empty-string element, it will also not match the null element (nothing matches null).

  • hash_structure.* or /hash_structure/*:
value 1
value 2

value 3
value 4

Notice that the empty value for "keyM" was included in the results. This is because -- against Hash data structures -- the * segment matches against key names, not their values.

Expand Collector Results

The * segment will also expand Collector results (breaking them out of Array form). For example, (array_values.*)+(hash_structure.*) normally returns a single Array match:

["alpha", "bravo", "charlie", "delta", "value 1", "value 2", "", "value 3", "value 4"]

Should you need to vertically expand the matches (say, to ingress them into an Array in a downstream process which doesn't readily parse JSON), you can add a * segment, like ((array_values.*)+(hash_structure.*))* or /((array_values.*)+(hash_structure.*))/* to produce instead:

alpha
bravo
charlie
delta
value 1
value 2

value 3
value 4

Match Every Leaf Node

This is a standalone ** (double star) as a YAML Path segment. It looks like some.path.** or /some/path/**. More segments may follow it.

This is a traversal segment which causes YAML Path operations to deeply traverse the document from that point. When there are no further segments in the YAML Path, every leaf node (Scalar value) is matched. When the YAML Path has at least one further segment, it (and all further segments) must match subsequent nodes (anywhere deeper than that point in the document) or none are matched. Results can be collected.

Match Every Leaf, No Matter How Deep

When you want every leaf node (Scalar value) within the document from any point defined by the YAML Path prefix, use **. When used at the document root, this will match every leaf node within the entire document.

Search Shorthand

The * character now also serves as a wildcard character for key-names, Hash values, and Array value comparisons, converting the segment to a Search Expression. For example, a YAML Path like abc.d* becomes abc[.^d], abc.*f becomes abc[.$f], and abc.*e* becomes abc[.=~/^.*e.*$/], and so on.

Clone this wiki locally