英文:
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> 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("(?<=[^0-9]),(?=[^0-9])")
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
(?<=[^0-9])\s*,\s*(?=[^0-9])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论