Sort out the best of the posts you have participated in to facilitate future reference.
Qianduo (flyinsky)I posted a post on the csdn forum asking: "How to retrieve the string to the array"
For example, read data from one row in a text file and save it to the table.
Aaa, BBB, CCC, DDD
01234, ABC, "AA, BB", ABC
In the second row, how can I retrieve the content in "" as a string.
In the past, the string. Split (",") is used to convert it into five fields.
SNTO)Two suggestions are proposed:
1. Replace the separator string, such as "01234, ABC," AA, BB ", ABC" --> "01234, ABC," AA | BB ", ABC"
2. perform a technical record when you find the '"' character, record location 1, and find the second '"'. Record Location 2, use functions such as substring to intercept data.
Sanniko)The suggestions are as follows:
1. Retrieve the first "Location
2. Retrieve the last "Location
3. Use string. substring
VinsonhwjWe found that "this comma-separated text format is a CSV file format ."
The code for reading CSV format is provided.
Neil_cn (Neil)The read code is also provided.
The above answers are all good, because I have used regular expressions several times, so the answer is:
Dim strpattern as string = "(" [W,] + "") | ([w] + ))"
Dim strline as string = "" AA, BB, C "", 01234, ABC, "" AA, BB "", ABC, X, "" A, BB """
Dim rxg as new system. Text. regularexpressions. RegEx (strpattern)
Dim resultcollection as system. Text. regularexpressions. matchcollection = _
Rxg. Matches (strline)
For each matchresult as system. Text. regularexpressions. Match in resultcollection
Debug. writeline (matchresult. Groups (0). tostring ())
Next
Explanation:
For such a complex string:
"AA, BB, C", 01234, ABC, "AA, BB", ABC, X, "a, BB"
According to its characteristics, it is either a string without "," or a string that may contain "," surrounded by quotation marks,
Use the following pattern matching:
("[/W,] +") | ([/W] + ))
The following result is displayed. Each result is divided into one row:
"AA, BB, C"
01234
ABC
"AA, BB"
ABC
X
"A, BB"
I thought this solved the problem perfectly, but qianduo (flyinsky) continued to ask:
"123", "456" "789"
123 is the first column 456 "789 is the second column
So how to use a regular expression?
The regular expression can still be used. See:
Use the following matching format:
([/W] +) | ("[/W,] +" [/W] *) +)
("[/W,] +" [/W] *) +) used to match the following format
"12, 9"
"456" "789"
"111" ADF "22" "333"
Is the following string complicated?
"123", "456" "789", "111", "333" ADF "22"
Matching result:
"123"
"456" "789"
"12, 9"
"111" ADF "22" "333"
The indomitable qianduo (flyinsky) has the following problems:
There is still a small problem ..
123, AAA, qwe,
In this case, there will be only three columns, instead of four columns, and the third column is empty.
Killer :(? <=...) And (? = ...)
After testing, the following mode can be used to solve the problem:
([/W] +) | ("[/W,] +" [/W] *) +) | ((? <= ,)(? =,) | (^ (? =,) | ((? <=,) $)
The key points are as follows:
"((? <= ,)(? =,) "Match ",,"
"(^ (? =,) "Match the first line ","
"((? <=,) $) "Matching the end of a row ","
For the following strings:
, "123", "456" "789", "123", 111, "333" ADF "22 ",,
The matching result is as follows:
<Blank>
<Blank>
"123"
"456" "789"
<Blank>
<Blank>
"12, 9"
123
<Blank>
"111" ADF "22" "333"
<Blank>
<Blank>
Conclusion:
The ability of regular expressions to match Dongdong cannot be underestimated ......