As the title states, I tried making regex for %George%<Croissant>|2|10.3$
The point is it has to match only the name between the both %
and the product between both <>
and then the numbers between |
However, if the name doesn't start with capital letters followed by only lowercase letters, it is invalid hence not matched. The regex I made is:
/%(?<name>[AZ][az]*)%<(?<product>[A-Za-z]*)>\|(?<quantity>(\d+))\|(?<price>(\d+\.\d*\$))/g
This is a match %George%<Croissant>|2|10.3$
(has proper name between the % which has a capital letter start start and followed to the end by lowercase letters), product (the product is between the < and >), a number between both | and final number (which is the final number and has to have $ in order to be valid)
This does NOT match %InvalidName%<Croissant>|2|10.3$
(because the name is invalid)
This does NOT match %Peter%<Gum>1.3$
(missing number from both |, it has to have 2 numbers.
Tested at regex101 and it matches only %George%<Croissant>|2|10.3$
.
%Valid%<Valid>valid|10|valid20$
is valid too because it has a Proper name (1 capital letter followed by only lowercase letters, has product which is < Valid
>, has number between | 10
| and has a number at the end |valid 20$
To match both patterns, you can match optional word characters \w*\|
before the pipe at group quantity , and match optional word chars without digits y>(\d+))
before group price .
To match both prices, you have to make the decimal part optional, because the pattern that you tried expects at least 1 or more digits and a dot using \d+\.\d*
You can also omit the extra capture group in quantity as the digits are already in a named capture group.
%(?<name>[A-Z][a-z]*)%<(?<product>[A-Za-z]*)>\w*\|(?<quantity>\d+)\|[^\W\d]*(?<price>(\d+(?:\.\d+)?\$))
Explanation
%(?<name>[AZ][az]*)%
Group name
between %
, match at least an uppercase char AZ and optional lowercase chars az <(?<product>[A-Za-z]*)>
Group product
Match optional chars A-Za-z \w*\|
Match optional word characters, then match |
(?<quantity>(\d+))
\|[^\W\d]*
Match optional word characters without the digits (to not overmatch the digits) (?<price>(\d+(?:\.\d+)?\$))
Group price, with an optional decimal part Try this one
%(?<name>[A-Z][a-z]*?)%<(?<product>[A-Za-z]*)>[^|]*?\|(?<quantity>\d+)\|[^\d]*(?<price>\d+(?:\.\d+)*\$)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.