我有一组以,
分隔的字符串
例如:
abc,defg,ijkl
pqrs,tu,vv
,klmnop,qwe
aamn,nn,khhk
如您所见,第三行不以子字符串开头。它以逗号开头。
使用正则表达式如何告诉字符串以第一个逗号之前的随机长度的子字符串开头。
答案 0 :(得分:2)
^"[^"]+",
此正则表达式将执行以下操作:
现场演示
https://regex101.com/r/kE3bG5/1
示例文字
abc,defg,ijkl
pqrs,tu,vv
,klmnop,qwe
aamn,nn,khhk
样本匹配
abc,
pqrs,
aamn,
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
[^,]+ any character except: ',' (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
答案 1 :(得分:1)
(?m)^[^,\w]*\w.*?,.*
解释
(?m) # Multi-line mode
^ # Beginning of line
[^,\w]* # Optional Non- word nor comma
\w # Word
.*? # Optional anything
, # Comma
.* # All to end of line