How to extract commas in between square brackets in notepad++?

隐身守侯 提交于 2021-02-17 03:39:11

问题


For example:

[TEXT1,TEXT2,TEXT3]

my expression: [\[].*,.*[\]]

Finds strings with commas (in between brackets,) but I only want to explicitly match the comma that exists in the square brackets.

I need to replace the commas with spaces - but only in the square brackets.

I've tried [\[],[\]] but that doesn't work -

\[(.*?)\] will find the text in between as well - but I do not want the entire string.

Can anyone suggest what I need to do to just find commas in between the brackets?


回答1:


Find what:

(?:\[|(?!^)\G)[^,\]]*\K,

Replace with:
space


Break-down:

  • (?:\[|(?!^)\G) Matches the opening [ or the end of last match (?!^)\G.
  • [^,\]]* Consumes all chars until the next comma.
  • \K Tells the regex engine to discard what it has matched so far.
  • , Finally matches the comma.



回答2:


I know this is an old question but I was looking to solve this same issue and found a solution with this: ,(?=[^\[]*\])

I hope it helps someone who stumbles on this post! 🙂

Note: Using Text Wrangler instead of Notepad++




回答3:


Square brackets generally need to be escaped in regex, so something like

\[.*(,).*\] 

could work - where the regex group #1 (which is the captured/replaceable part in other tools, I don't have notepad++) would be the single comma in brackets that you want.




回答4:


I used this:

(?<=[\[,])[^\],]*(,)

Regex101



来源:https://stackoverflow.com/questions/32893701/how-to-extract-commas-in-between-square-brackets-in-notepad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!