我有下面这段字符串
text := 59-1124043-1053 - FLOATING JOINT4PC15-Feb-2020 Purchase PriceUSD 28.00112.00per 1 PCTaxable: N Resale: N<70.000KG DHL Express - Intl Shpts 962183650SCAC1:DHLCOver70.000KG Agility Logistics - Export StandardSCAC2:AGYC3 59-1124043-1055 - UNDER RAIL1PC15-Feb-2020 Purchase PriceUSD 138.00138.00per 1 PCTaxable: N Resale: N<70.000KG DHL Express - Intl Shpts 962183650SCAC1:DHLCOver70.000KG Agility Logistics - Export StandardSCAC2:AGYC4 59-1124043-1056 - UPPER RAIL1PC15-Feb-2020
通过正则规则
PNExprV2 = [^(-138)][\d]+-.*[\d][\s]+.*[\d]{1}[a-zA-Z]{2}[\d]{2}-[a-zA-Z]{3}-[\d]{4}
PNParser := regexp.MustCompile(PNExprV2)
line1 := PNParser.FindAllStringSubmatch(c, 10)
匹配出来的还是原字符串 期待匹配结果是 ["59-1124043-1053 - FLOATING JOINT4PC15-Feb-2020", ..., "59-1124043-1056 - UPPER RAIL1PC15-Feb-2020"]
请大佬们帮忙看看
1
buhuiqizi 2020-03-21 10:30:45 +08:00
.*就已经匹配完了后面的所有字符串。
[^(-138)][\d]+-[\d]+-[\d]+\s-\s[a-zA-Z]+\s[a-zA-Z0-9]+-[a-zA-Z]+-[\d]+ 这个能匹配出来,但是我也是个菜鸟,还是等大佬的吧。 |
2
Akiyu 2020-03-21 10:34:12 +08:00
有什么特殊要求么?
[^(-138)] 看起来你想不以 -, 1, 3, 8 开头? 那么不需要括号, 直接 [^-138] 就好. 除非你是想不以整个 -138 开头. 其他方便好像没看出什么来. 简单的话就 "\d+-\d+-\d+ - \w+ \w+-\w{3}-\d{4}" PS: 不要滥用 .* 测试可以用这个网站: https://regex101.com/ |
5
Akiyu 2020-03-21 15:58:50 +08:00
"\d+-\d+-\w+( - \w+)? \w+-\w{3}-\d{4}"
"611-0571271-20126 8PC03-Feb-2020" 这个字符串中间没有之前 "59-1124043-1053 - FLOATING JOINT4PC15-Feb-2020" 中的 " - FLOATING" 改成可选的就行. 然后 "811-0571378-5147B 7PC03-Feb-2020" 中的 "5147B" 出现了字母, 用字符匹配. 推荐一下 <regular expression> 这本书. 简单的话 <正则表达式必知必会> 就几十页, 上班通勤就完全搞定. 基本上大多简单的问题都能解决. |
6
wudaown OP @Akiyu 感谢
我这个比较复杂并且不是很通用 如下字符 ··· 611-0571271-20126 8PC03-Feb-2020DEBURR INSERTMaterial revision level: A Purchase PriceUSD 14.00112.00per 1 PCTaxable: N Resale: NRFQ# 170593<70.000KG DHL Express - Intl Shpts 962183650SCAC1:DHLCOver70.000KG Agility Logistics - Export StandardSCAC2:AGYC711-0571337-21531 5PC03-Feb-2020U-ING PUNCHMaterial revision level: B Purchase PriceUSD 41.00205.00per 1 PCTaxable: N Resale: NRFQ# 170593<70.000KG DHL Express - Intl Shpts 962183650SCAC1:DHLCOver70.000KG Agility Logistics - Export StandardSCAC2:AGYC811-0571378-5147B 7PC03-Feb-2020 59-1124043-1053 - FLOATING JOINT4PC15-Feb-2020 Purchase PriceUSD 28.00112.00per 1 PCTaxable: N Resale: N<70.000KG DHL Express - Intl Shpts 962183650SCAC1:DHLCOver70.000KG Agility Logistics - Export StandardSCAC2:AGYC3 59-1124043-1055 - UNDER RAIL1PC15-Feb-2020 Purchase PriceUSD 138.00138.00per 1 PCTaxable: N Resale: N<70.000KG DHL Express - Intl Shpts 962183650SCAC1:DHLCOver70.000KG Agility Logistics - Export StandardSCAC2:AGYC4 59-1124043-1056 - UPPER RAIL1PC15-Feb-2020 219-1895504 Die Sleeve, Floating Die Case6PC31-Jan-2020 Purchase PriceUSD 117.00702.00per 1 PCTaxable 119-1895433-00008 Punch Cap6PC31-Jan-2020 Purchase PriceUSD 95.0 are met for this material / product. This specification is available under Documents in the TE SupplierPortal at https://supplierportal.te.comGD40 - EACH LOT MUST INCLUDE AN INSPECTION REPORT, WITH 100% INSPECTION OF 10% OF THE PIECES, WITH A MINIMUM OF ONE PIECE INSPECTED AND A MAXIMUM OF 3 PIECES INSPECTED FOR EACH LOT.GD50 - EACH PIECE OF TOOLING TO BE CONSTRUCTED PER TE DRAWING AND SCRIBED IN A NON-FUNCTIONAL AREA WITH THE DETAIL NUMBER, REVISION LEVEL AND SUPPLIER NUMBER. SEE PURCHASE ORDER FOR SUPPLIER NUMBER. IF SIZE IS RESTRICTED, BAG AND TAG EACH PIECE WITH THE SAME INFORMATION.GE26 - THE VALUE OF THIS PO IS NOT CONSIDERED AN "ASSIST" BY U.S.CUSTOMS.119-1895433-00008 Punch Cap6PC31-Jan-2020 219-1895504 Die Sleeve, Floating Die Case6PC31-Jan-2020 ··· |
7
wudaown OP 我最后用了
(\d+-\d+-[\d]+|\d+-\d+)(.*?)\d{1,2}\w{2}\d{2}-\w{3}-\d{4} |