正则表达式 [英] Regex Wrapping Quotes

查看:258
本文介绍了正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在CSV文件中的某些部分的内容中加上引号,当前的布局如下:

  ### element1,element2,element3,element4,element5,element6,element7,element8,element9,
element9,element9,element9,
element9,element9, element10,
###

###符号表示一个新行,行应该有一个,问题是我需要获得到所有的元素9在一组双引号,但是在该区域内的双引号的多个实例将该元素拆分到新的字段,使我的表扩展超出字段我最初设置。所以我相信我需要删除element9的开始和结束之间的所有标记,然后重新引入一组突出整个部分。



我接近这首先尝试从开始选择第8个逗号,从结尾选择2个逗号:

  ^((?:[^,] + ,){8})(。+)((?:,[^,] *){2})$ 

并替换为

  $ 1$ 2$ 3 
pre>

我尝试指定开始###和结束###来选择这两个元素,但没有成功。



有关如何执行此操作的任何建议



UPDATE

  ### BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,
BLAHBLAH,
BLAHBLAH,
BLAHBLAH,BLAHBLAH ,
BLAHBLAH,BLAHBLAH,
BLAHBLAH,
BLAHBLAH,E,
###

最后一个字段总是包含一个大写字母,字段前面不同的报价布局,所以真正目标的整个部分,我需要解决多少个逗号和多少

解决方案

  ###(?:[^,] *,){8} \K([\s\S] *? ##)

尝试此操作以\1 code>或$ 1。参阅演示。



https://regex101.com/r/tD0dU9/13


I am trying to wrap quotes around certain section of content in a CSV file, the current layout is something like this:

 ###element1,element2,element3,element4,element5,element6,element7,element8, "element9,
element9,""element9"",element9,
element9,element9,""element9",element10,
###

the ### symbols depict a new line and each new line should have one, the problem is I need to get to all of element 9 in to one set of double quotes, however there are multiple instances of doublequotes within that area which break up the element in to new fields making my table expand beyond the fields I initially set. So I believe I need to remove all the " marks between the start and end of element9 and then reintroduce one set to highlight the whole section.

I approached this firstly by trying to select the 8th Comma from the start and the 2 comma from the end:

 ^((?:[^,]+,){8})(.+)((?:,[^,]*){2})$

and replacing with

$1"$2"$3

I tried to target the starting ### and ending ### to select those two elements but with no success.

any suggestions on how I can do this

UPDATE

    ###BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,BLAHBLAH,
BLAHBLAH,
BLAHBLAH,
BLAHBLAH, BLAHBLAH,
BLAHBLAH, BLAHBLAH,
BLAHBLAH,
"BLAHBLAH""",E,
###

The last field always seem to contain a capital letter, the fields before vary in quotation placement so to really target that whole section I need to work out how many commas along and how many back I need to go, remove the quotes and then reinstate them in the correct positions.

解决方案

###(?:[^,]*,){8}\K([\s\S]*?)(?=,[^,]*,[^,]*?###)

Try this.Replace by "\1" or "$1".See demo.

https://regex101.com/r/tD0dU9/13

这篇关于正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆