[英]Javascript regex for words between characters
As the title states, I tried making regex for %George%<Croissant>|2|10.3$
正如标题所述,我尝试为%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 |这是匹配%George%<Croissant>|2|10.3$
(在 % 之间有专有名称,% 以大写字母开头,后面跟着小写字母),产品(产品在 < 和 > 之间) , 两者之间的数字 | 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)这与%InvalidName%<Croissant>|2|10.3$
不匹配(因为名称无效)
This does NOT match %Peter%<Gum>1.3$
(missing number from both |, it has to have 2 numbers.这与%Peter%<Gum>1.3$
不匹配(两个 | 中都缺少数字,它必须有 2 个数字。
Tested at regex101 and it matches only %George%<Croissant>|2|10.3$
.在 regex101 上测试,它只匹配%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$
%Valid%<Valid>valid|10|valid20$
也是有效的,因为它有一个专有名称(1 个大写字母后跟小写字母,产品为 < Valid
>,编号介于 | 10
| 之间,编号位于结束|有效20$
To match both patterns, you can match optional word characters \w*\|
要匹配这两种模式,您可以匹配可选的单词字符\w*\|
before the pipe at group quantity , and match optional word chars without digits y>(\d+))
before group price .在组quantity的 pipe 之前,并匹配组price之前没有数字y>(\d+))
的可选单词字符。
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*
要匹配这两个价格,您必须将小数部分设为可选,因为您尝试的模式需要至少 1 个或多个数字和一个使用\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 %(?<name>[AZ][az]*)%
%
的组name
,至少匹配一个大写字符 AZ 和可选的小写字符 az<(?<product>[A-Za-z]*)>
Group product
Match optional chars A-Za-z <(?<product>[A-Za-z]*)>
product
组匹配可选字符 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) \|[^\W\d]*
匹配不带数字的可选单词字符(不超过数字)(?<price>(\d+(?:\.\d+)?\$))
Group price, with an optional decimal part (?<price>(\d+(?:\.\d+)?\$))
组价,小数部分可选Try this one试试这个
%(?<name>[A-Z][a-z]*?)%<(?<product>[A-Za-z]*)>[^|]*?\|(?<quantity>\d+)\|[^\d]*(?<price>\d+(?:\.\d+)*\$)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.