I have a regex problem which bugs me and have no clue how to solve it.
I have an input field with a text and I like to extract certain values out of it.
I would like to extract a title, description, a price and a special price.
Examples for the input:
- everything what is plain text is concerned as title.
- everything within within hashes (#description goes here#) is considered the description.
- $23.49 is considered as price and %$19.99 would match the special price.
The CoffeeScript pattern I’m using:
pattern = ///
([^$]+)
(#(.+?)#+)
([\$]\d+\. \d+)
([\%\$]\d+\. \d+)
///
params = [title,description,oldPrice,newPrice]=input_txt.match(pattern)[1..4]
It does not work. It should work if I enter all values in the given sequence and I also have to provide a the asked substring.
What I would like to have is the ability to extract the sequments if the are provided (so optional) and no matter of the sequence…
How can I extract optional sequences of an string…
EDIT///
I provide some examples
exmp1:
Kindle #Amazon's ebook reader# $79.00
this should be extracted as
title:Kindle
description: Amazon's ebook reader
oldPrice:$79.00
exmp2:
Nike Sneaker's $109.00 %$89.00
this should be extracted as
title:Nikes Sneaker's
oldPrice:$109.00
newPrice:$89.00
exmp3:
$100.00 Just dance 3 #for XBox#
this should be extracted to
title: Just dance 3
description: for XBox
oldPrice:$100.00
Any help would be great …
The nature of regular grammars makes it hard to solve your problem. As a work around the simplest solution would be to just execute your regex 4 times: