Easiest way to eliminate certain pattern in a text file
I have a vCard file containing thousands of contacts. In a vCard file contacts are stringed together and a single contact is embraced by
BEGIN:VCARD
...
END:VCARD
Companies or organizations 开发者_Go百科can be set by
ORG;CHARSET=UTF-8:My Company;
I am looking for the easiest way to eleminate every entry that has no company. I don't mind if it's a script language or even Notepad++.
Any suggestions? Thank you!
Have you thought about Regular Expressions?
You could create a Regex that requires the company field, and then store the resulting captures. Any element that doesn't have the company field would not be captured and would thus be removed.
For example:
BEGIN:VCARD
...
ORG;CHARSET=UTF-8:My Company;
...
END:VCARD
BEGIN:VCARD
...
...
END:VCARD
BEGIN:VCARD
...
ORG;CHARSET=UTF-8:My Company;
...
END:VCARD
You can capture an individual vCard with: BEGIN:VCARD\s+(.*?$\s+)+?END:VCARD
Then with each match, if the captured string is also a match to: BEGIN:VCARD\s+(.*?$\s+)+ORG;CHARSET=UTF-8:(?<companyName>.*?);\s+(.*?$\s+)+END:VCARD
then keep it. If it isn't a match, there is no companyName, so don't save it.
I'm sure you can do it with one Regex, but this works for me by breaking down the problem
精彩评论