Regex : how to find sub group of items

Hi everyone,
this one is surely for Kem but who knows … :wink:

on this kind of text datas :

Specifications:
  CPUCodename:
    - "Cascade Lake"
  CPU:
    - "Intel Xeon W-3245M CPU @ 3.20 GHz"
  GPU:
    - "Radeon Pro 580X"
    - "Radeon Pro W5700X"
    - "Radeon Pro Vega II"
    - "Radeon Pro Vega II Duo"
  RAM:
    - "32GB DDR4 ECC"
  SystemReportName:
    - "Mac Pro (2019)"
  MarketingName:
    - "Mac Pro (2019)"
# Note, first model code is used by macserial
AppleModelCode:
  - "P7QM"
  - "PLXV"
  - "PLXW"
  - "PLXX"
  - "PLXY"
  - "P7QJ"
  - "P7QK"
  - "P7QL"
  - "P7QN"
  - "P7QP"
  - "NYGV"
  - "K7GF"
  - "K7GD"
# Note, first board code is used by macserial
AppleBoardCode:
  - "K3F7"
# Note, first year is used by macserial
AppleModelYear:
  - 2019
  - 2020

how can I extract all the applemodelcodes one by one with a regex ?
some sort of sub-grouping ?
can’t figure it.

thanks.

1 Like

My “like” as it is a cool question and I’m interested as well. Now waiting for Kem :slight_smile:

First, a suggestion…

This looks like YAML, a structured data format related to JSON. As Xojo has no native YAML parser, your best option is to output this in a format Xojo does understand like JSON or XML. The second best option is to get a YAML parser plugin like… well, I know of none, but that doesn’t mean they aren’t out there. You might also be able to find a command-line tool to do the job and run that through a Shell.

But to answer your question…

Do it in two passes. First, get the block:

AppleModelCode:\R((?:^ +-.*\R)+)

In SubExpressionString( 1 ) you’ll find the list of codes:

  - "P7QM"
  - "PLXV"
  - "PLXW"
  - "PLXX"
  - "PLXY"
  - "P7QJ"
  - "P7QK"
  - "P7QL"
  - "P7QN"
  - "P7QP"
  - "NYGV"
  - "K7GF"
  - "K7GD"

Now you can either Split and massage the data in code, or use this pattern:

^ +- +"([^"]+)

The codes will be in SubExpressionString( 1 ) of each match.

2 Likes

(I keep meaning to write a YAML class, but it’s complicated, and the best bet would be something that works around one of the existing libraries.)

thanks for this Kem, I could have found it this way.
so there is no way to get it using a regex in one pass only ?

1 Like

I can’t think of one. You could eliminate the first by splitting on the AppleModelCode key first, but that’s essentially the same thing.

1 Like