How to parse a String?

Hi all!

How can I parse a String, I know that that exists split and NthField, but what do you suggest?

that is an example of my String: Quantity: 10 Measurement: Pieces Description: blablabla UnitPrice: 58.80 TotalPrice: 180.00 Quantity: 10 Measurement: Pieces Description: blablabla UnitPrice: 58.80 TotalPrice: 180.00 .

As you can see it repeats depending if there’s more than one item.

Thanks

Or can your description contain spaces? Are your keywords (quantity etc) always the same? Can the value of the description contain a keyword or a “:”?

I’d split by space. Then loop through the resulting array. If you find a keyword ending with a “:” then you know that the next entry contains a value.

HTH

[quote=200272:@Beatrix Willius]Or can your description contain spaces? Are your keywords (quantity etc) always the same? Can the value of the description contain a keyword or a “:”?

I’d split by space. Then loop through the resulting array. If you find a keyword ending with a “:” then you know that the next entry contains a value.

HTH[/quote]
No, the values always change, So I think about the description contains spaces and If I split by spaces, it will split in many many pieces. So I thought to put commas and split by commas.

Like this:

Concept = “Quantity: 1, Measurement Unit: PIECE, Descriptión: FH-8M FLEXOMETER, UnitPrice: 128.21, TotalPrice: 128.21”
Dim anArray(-1) as String
anArray=Concept.Split(",")

r.CellText(r.LastIndex, 0) = anArray(0)
r.CellText(r.LastIndex, 1) = anArray(1)
r.CellText(r.LastIndex, 2) = anArray(2)
r.CellText(r.LastIndex, 3) = anArray(3)
r.CellText(r.LastIndex, 4) = anArray(4)

Now I get splited in four pieces, Its fine. Now I need to trim the labels: Quantity, Measurement Unit, UnitPrice and blah blah blah

Now I’m thinking to use LEFT to trim the text.

And As I say in the String repeats the values as many Items has inside.
So I dunno If a loop will be a good idea, the issue is how to know, where to stop.

Cuz Sometimes will have 1 items, sometimes 50, sometimes 80, sometimes 18, etc.

If this is your format then you can design something that is easier to parse. Like Json or xml.

Have you considered using a regular expression?

dim rx as new RegEx
rx.SearchPattern = "Quantity: (\\d+) Measurement: (\\w+) Description: (\\w+) UnitPrice: ([\\d.]+) TotalPrice: ([\\d.]+)"

dim match as RegExMatch = rx.Search( concept )
while match isa RegExMatch
  quan = rx.SubExpressionString( 1 )
  meas = rx.SubExpressionString( 2 )
  // etc

  match = rx.Search
wend

[quote=200278:@Kem Tekinay]Have you considered using a regular expression?

[code]
dim rx as new RegEx
rx.SearchPattern = “Quantity: (\d+) Measurement: (\w+) Description: (\w+) UnitPrice: ([\d.]+) TotalPrice: ([\d.]+)”

dim match as RegExMatch = rx.Search( concept )
while match isa RegExMatch
quan = rx.SubExpressionString( 1 )
meas = rx.SubExpressionString( 2 )
// etc

match = rx.Search
wend
[/code][/quote]
Its said me that error: Type RegEx has nonmember called “SubExpressionString”
rx.SubExpressionString( 1 )

Whats wrong?

Better use Tab Chr(9) ; you may have comma in the description and that would throw your format off.

Gerardo:

since Delimiter can be one or more characters and does not appears when you split the string…

But this depends if you are using this “string” else where.

If you only use it once, build the original string using your own delimiter (Axell can suggest two pipes characters || ) and use these two characters as the field delimiter.

Dim anArray(-1) as String
Dim indx as integer
anArray=Concept.Split(chrB(9))
for indx=0 to anArray.Ubound
r.CellText(r.LastIndex, indx) = anArray(indx)
next indx

[quote=200278:@Kem Tekinay]Have you considered using a regular expression?

[code]
dim rx as new RegEx
rx.SearchPattern = “Quantity: (\d+) Measurement: (\w+) Description: (\w+) UnitPrice: ([\d.]+) TotalPrice: ([\d.]+)”

dim match as RegExMatch = rx.Search( concept )
while match isa RegExMatch
quan = rx.SubExpressionString( 1 )
meas = rx.SubExpressionString( 2 )
// etc

match = rx.Search
wend
[/code][/quote]
Probed with quan = match.SubExpressionString( 1 )
meas = match.SubExpressionString( 2 )

And It Doesn’t evaluate nothing.

So I probe to start to evaluate only the first Item: Quantity.

And It does!, Ex. If I have 8 items with Quantity, it prints the 8 values related to Quantity.

The second probe is: Evaluate the second field: Unidad.

But Only prints one value from Unidad and one value from Quantity.

Using this:
rg.SearchPattern=“Cantidad: (\d+) Unidad: (\w+)”

I used the data you posted as a template. You’d have to tweak the pattern to suit your needs, or post more data here.

Hi Kem,thanks. This is my Data:

Quantity: 1 Measurement: PIEZA Description: FH-8M FLEXOMETRO UnitPrice: 128.21 TotalPrice: 128.21 Quantity: 7 Measurement: ROLLO Description: MALLA SOLDADA 66-66 2.5M X 40M UnitPrice: 2160.00||TotalPrice: 15120.00 Quantity: 200 Measurement: Pounds Description: First Clase Elizondo Flour UnitPrice: 3000.00||TotalPrice: 300000.00

And I want to get this result:

Quantity Measurement: Description: UnitPrice: TotalPrice:
1 PIEZA FH-8M FLEXOMETRO 128.21 128.21
7 ROLLO MALLA SOLDADA 66-66 2.5M X 40M 2160.00 15120.00
200 Pounds First Clase Elizondo Flour 3000.00 300000.00

Curious how the OP seems obsessed with regex…

I made one more test, with three values: rg.SearchPattern=“Cantidad: (\d+) Unidad: (\w+) Descripcin: (\w+)”

It works perfectly probing to put simple words on Description, like Potatoes, Carrots, Tomatoes.
But when I try to put words with spaces, ex. Fried Potatoes, Carrots soup, Smashed Tomatoes; Only prints: Fried, Carrots and Smashed

In the data you posted, the separator in the later data seems to be double bars ("||") rather than a space. Was that a typo?

Double Bars

OK, this pattern works for either:

(?U)Quantity: (.+) Measurement: (.+) Description: (.+) UnitPrice: (.+)(?: |\\|\\|)TotalPrice: (.+)(?= Quantity:|$)

Also I noticed that when I add this to my Search Pattern, It gets the second word of the Phrase, the issue is when I get a big Description:

Description: (\w+\s+\w+)"

To be fair though, this is exactly the type of problem that regular expressions are suited.

So, My Concept must be set as this?:

Concept = Quantity: 1||Measurement: PIEZA||Description: FH-8M FLEXOMETRO||UnitPrice: 128.21||TotalPrice: 128.21||Quantity: 7||Measurement: ROLLO||Description: MALLA SOLDADA 66-66 2.5M X 40M||UnitPrice: 2160.00||TotalPrice: 15120.00||Quantity: 200||Measurement: Pounds||Description: First Clase Elizondo Flour||UnitPrice: 3000.00||TotalPrice: 300000.00

I set Conceptos as this way,

I’m testing with two fields at this time:
(?U)Quantity: (.+) Measurement: (.+)

AT Quantity its OK, but In Measurement Only I get the first Letter: Ex. PIECE ---- P