简体   繁体   English

VB.net中的正则表达式问题,我该如何匹配?

[英]Regex problems in VB.net, how do I match this?

I never understand how to create regular expressions and now I need one badly. 我从不了解如何创建正则表达式,现在我急需一个。 It would be great if someone know how to do this. 如果有人知道该怎么做,那将是很棒的。

I need to match these examples with a regex and then append text before the third comma: Examples: 我需要将这些示例与正则表达式匹配,然后在第三个逗号之前附加文本:

1. 1。

Örjan,,;Svensson,,,,, and then it continues like this Örjan,,; Svensson ,,,,然后像这样继续

Needs to become: 需要成为:

Örjan,,;SvenssonNEWTEXTHERE,,,,, and then it continues like this Örjan,;; SvenssonNEWTEXTHERE ,,,,然后像这样继续

2. 2。

Patric,The-Man,Black,,,,,,,,, and then it continues like this Patric,The-Man,Black ,,,,,,,,,然后像这样继续

Needs to become: 需要成为:

Patric,The-Man,BlackNEWTEXTHERE,,,,,,,,, and then it continues like this Patric,The-Man,BlackNEWTEXTHERE 、、、、、、、,然后像这样继续

If I would use wildcards to do this it would look like this: 如果我要使用通配符来执行此操作,则它将如下所示:

*,*,*,*

And I would like to add text just before the last comma. 我想在最后一个逗号之前添加文本。 But I still need the whole string so the text can just be added there I don't want the characters that comes after the added text to disappear. 但是我仍然需要整个字符串,以便可以仅在此处添加文本,我不希望添加的文本后面的字符消失。

This is a .CSV contact file btw so you better understand the structure of the text. 这是一个.CSV联系人文件,因此您可以更好地了解文本的结构。

Is this possible? 这可能吗?

The regular expression for a CSV field, ie “any text not containing comma”, is [^,]* , if you want to skip to the end of the third field, you'll use CSV字段的正则表达式(即“不包含逗号的任何文本”)为[^,]* ,如果您想跳到第三个字段的末尾,请使用

[^,]*,[^,]*,[^,]*

Now, if you want to modify the string, you can use something like 现在,如果要修改字符串,可以使用类似

Dim str = "Örjan,,;Svensson,,,,, and then it continues like this"
Dim re As New Regex("[^,]*,[^,]*,[^,]*")
Dim pos = re.Match(str).Length

Now you've got the position of where you want to put the additional string in pos , and you can do whatever you want with it. 现在,您已经可以将其他字符串放在pos ,并且可以使用它进行任何操作。

Note that a CSV file can generally contain fields which contain literal commas and need to be quoted (eg Patric,"The,Man",Black,... ). 请注意, CSV文件通常可以包含包含文字逗号且需要用引号引起来的字段(例如Patric,"The,Man",Black,... )。 It may even contain a linebreak, which makes it quite difficult to parse properly, especially with regular expressions (and the code above would not work with such data). 它甚至可能包含换行符,这使得很难正确解析,尤其是对于正则表达式(并且上面的代码不适用于此类数据)。 Can you be sure your CSV file does not contain quoted fields? 您可以确定CSV文件中不包含带引号的字段吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM