简体   繁体   English

RegEx 替代lookbehind

[英]RegEx alternative to lookbehind

I have the following text:我有以下文字:

asdfasdf added Big Box of Chocolate Bars x9, Bottle of Beer x10, Empty Blood Bag x4 to the trade.
asdfasdf added Grenade x8, Pepper Spray x7 to the trade.
tasdf added Jacket x6 to the trade.
aasdfa added Banana Orchid x5 to the trade.
asdfasdas added Single Red Rose x4 to the trade.
dgfhdfggg added Bunch of Flowers x3 to the trade.
asdfax2sdfas added Blood Bag : A+ x2 to the trade.
asdasdasd added Empty Blood Bag x1 to the trade.
Monkey Plushie x10, Leather Gloves x12, Monkey Plushie X321

I want to capture the item names and the quantity, Monkey Plushie x10 , Single Red Rose x4 , etc. At the moment I am using positive lookbehind to do it but it seems that it's not available on react native so I need an alternative to it.我想捕获项目名称和数量, Monkey Plushie x10Single Red Rose x4等。目前我正在使用积极的后视来做到这一点,但它似乎不适用于 react native,所以我需要一个替代方案. Best I got so far is (?:added)(\s.+x\d+) ...yes I am terrible at regex...到目前为止我得到的最好的是(?:added)(\s.+x\d+) ...是的,我在正则表达式方面很糟糕...

Split by newlines, then match the first two words in the line, while capturing the rest of the line (except the to the trade part, which we want to exclude, so match it normally).用换行符分割,然后匹配该行的前两个单词,同时捕获该行的rest(除了我们要排除to the trade部分,所以正常匹配)。 Then extract the captured group and split by a comma and a space:然后提取捕获的组并用逗号和空格分隔:

 const input = `asdfasdf added Big Box of Chocolate Bars x9, Bottle of Beer x10, Empty Blood Bag x4 to the trade. asdfasdf added Grenade x8, Pepper Spray x7 to the trade. tasdf added Jacket x6 to the trade. aasdfa added Banana Orchid x5 to the trade. asdfasdas added Single Red Rose x4 to the trade. dgfhdfggg added Bunch of Flowers x3 to the trade. asdfax2sdfas added Blood Bag: A+ x2 to the trade. asdasdasd added Empty Blood Bag x1 to the trade.`; const output = input.split('\n').flatMap( line => line.match(/\S+ \S+ (.*) to the trade\./)[1].split(', ') ); console.log(output);

If you can't use flatMap , then spread into concat instead:如果您不能使用flatMap ,则改为使用concat

 const input = `asdfasdf added Big Box of Chocolate Bars x9, Bottle of Beer x10, Empty Blood Bag x4 to the trade. asdfasdf added Grenade x8, Pepper Spray x7 to the trade. tasdf added Jacket x6 to the trade. aasdfa added Banana Orchid x5 to the trade. asdfasdas added Single Red Rose x4 to the trade. dgfhdfggg added Bunch of Flowers x3 to the trade. asdfax2sdfas added Blood Bag: A+ x2 to the trade. asdasdasd added Empty Blood Bag x1 to the trade.`; const addedsArrOfArrs = input.split('\n').map( line => line.match(/\S+ \S+ (.*) to the trade\./)[1].split(', ') ); const output = [].concat(...addedsArrOfArrs); console.log(output);

If you can't use Javascript string/array manipulation at all, you can use the regular expression如果你根本不能使用 Javascript 字符串/数组操作,你可以使用正则表达式

\b(?!.*added)\w[^,\n]*(?=.*x\d)x\d+

https://regex101.com/r/b6rYvA/1/ https://regex101.com/r/b6rYvA/1/

but that's ugly and a lot harder to understand at a glance, so I wouldn't recommend it.但这很丑,而且一目了然很难理解,所以我不推荐它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM