正则表达式,用于逗号分隔的文本,但不包围在数字中。

huangapple go评论73阅读模式
英文:

Regular expression for comma separated text but not surround with digits

问题

I need to split the text with comma separated but not with surround digit like below

Text is LOCAL_GUA_CONTRACT_AMT NUMBER(22,3) , LOCAL_GUA_CONTRACT_AMT NUMBER(22,3)

and output is LOCAL_GUA_CONTRACT_AMT NUMBER(22,3)
                LOCAL_GUA_CONTRACT_AMT NUMBER(22,3)

英文:

I need to split the text with comma separated but not with surround digit like below<br><br>
Text is LOCAL_GUA_CONTRACT_AMT NUMBER(22,3) , LOCAL_GUA_CONTRACT_AMT NUMBER(22,3)<br><br>
and output is LOCAL_GUA_CONTRACT_AMT NUMBER(22,3)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOCAL_GUA_CONTRACT_AMT NUMBER(22,3)

答案1

得分: 1

使用这个正则表达式:(?<=[^0-9]),(?=[^0-9])

text.split("(?<=[^0-9]),(?=[^0-9])")

它会将逗号作为分隔符进行拆分,但是如果旁边是数字,则不会拆分。但是它会保留空格,就像它们原本的样子一样。

如果你想要删除空格,也可以使用这个正则表达式:

(?<=[^0-9])\s*,\s*(?=[^0-9])
英文:

Use this regex: (?<=[^0-9]),(?=[^0-9])

text.split(&quot;(?&lt;=[^0-9]),(?=[^0-9])&quot;)

it will split with comma as a separator but never if numbers are next to it. But it will leave whitespaces as they were

And if you want whitespaces to be deleted also, use this

(?&lt;=[^0-9])\s*,\s*(?=[^0-9])

huangapple
  • 本文由 发表于 2020年9月11日 14:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定