Note that there are some explanatory texts on larger screens.

plurals
  1. PORegular expression to parse YAML list
    primarykey
    data
    text
    <p>I've got a YAML list like this and need to parse it with a regular expression in Java:</p> <pre><code>stacks: - name: test1 folder: a - name: test2 folder: b </code></pre> <p>In the end I would like to iterate the list text blocks. That means after creating a Pattern instance, I'd like the Matcher instance for this text to find two matches:</p> <p>1.</p> <pre><code>name: test1 folder: a </code></pre> <p>2.</p> <pre><code>name: test2 folder: b </code></pre> <p>When ignoring the list name ("stacks" above), I could do the following:</p> <pre><code>String regex = "^\\s++-(.*$\\n(?:^\\s++[^-].*$\\n)*)"; Pattern p = Pattern.compile( regex, Pattern.MULTILINE | Pattern.UNIX_LINES ); Matcher m = p.matcher( data ); </code></pre> <p>A matcher generated with this expression would find the two matches that I need.</p> <p>However, I actually don't want to ignore the list name and I would like to state "take all list elements after the following list name" (like a method call <code>getListData(yamlData, listName)</code>). It seems that I would need a positive backward lookbehind. Unfortunately, I didn't find a way to do this while still getting two matches. Do you have any idea?</p> <p><em>(I know there is snakeyaml and other libraries. Unfortunately, I need to use regular expressions here.)</em></p> <p><strong>Edit</strong>: I can't make any assumptions about the order of the elements of the listed objects. A solution must be able to deal with name and folder (and potentially others) swapped.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload