https://kotlinlang.org logo
Title
m

Mani

11/21/2019, 7:22 PM
Folks, need quick help. I am bad with regex. I have a bunch of strings like this
Ivy Gourd- 2 kg fav_variety : Fresh fav_quantity : 2 kg pcp_packaging_type : Loose packing fav_unit_of_measurement : Per kg fav_average_weight : 2 kg is_assortment : false num_items_assortment : 0 length_mm : 250 breadth_mm : 180 height_mm : 120 dead_weight```
a

Andy Gibel

11/21/2019, 7:25 PM
off the top of my head -
(.*)- [0-9]
?
oh wait you need the unit and number too
m

Mani

11/21/2019, 7:26 PM
yep
r

Ruckus

11/21/2019, 7:26 PM
Why are you using regex? Why not just use
string.subString(0, string.indexOf("fav-variety"))
?
m

Mani

11/21/2019, 7:27 PM
thats a good one, lemme check
a

Andy Gibel

11/21/2019, 7:27 PM
(.*- [0-9]{1} [a-z]{2})
m

Mani

11/21/2019, 7:27 PM
@Andy Gibel that works for this example, rest can have different names and diff numbers
however
fav_variety
string is guranteed
a

Andy Gibel

11/21/2019, 7:28 PM
This works for any names, it just assumes first number is single digit and unit is 2 characters
oh the dash is not special - it's part of the first name?
then yeah I guess split on
fav_variety
m

Mani

11/21/2019, 7:32 PM
Thanks folks, it worked 🙂