While parsing CSV file many times we need to replace the comma outside the double quotes by some other characters like pipe(|).
We can do that by using regular expression in a very simple way.
Lets see some example:
Input String: Japan,"Washington, DC,Prabhu,aju",New York,"Beijing, shanghai",Tokyo,Delhi
Out Put String: Japan|"Washington, DC,Prabhu,aju"|New York|"Beijing, shanghai"|Tokyo|Delhi
Input String: "abc,def","xyz,",",def",""
Out put String: "abc,def"|"xyz,"|",def"|""
If we will do this by JavaScript then:
Use the Regular Expression: ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"
<script type="text/javascript">
var inputStr = "";//Your String
var result = Regex.Replace(inputStr, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)","|");//Resulted String
</script>
This produce the string with all comma(,) present out side the double quote will be replaced by pipe(|).
This question is answered at this :
http://www.mindfirelabs.com/forum/viewtopic.php?f=13&t=481
Hope it will save your ...
No comments:
Post a Comment