Folks, need quick help. I am bad with regex. I hav...
# getting-started
m
Folks, need quick help. I am bad with regex. I have a bunch of strings like this
Copy code
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
off the top of my head -
(.*)- [0-9]
?
oh wait you need the unit and number too
m
yep
r
Why are you using regex? Why not just use
string.subString(0, string.indexOf("fav-variety"))
?
m
thats a good one, lemme check
a
(.*- [0-9]{1} [a-z]{2})
m
@Andy Gibel that works for this example, rest can have different names and diff numbers
however
fav_variety
string is guranteed
a
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
Thanks folks, it worked 🙂