0

I need to knowingly isolate each row of the vCard and get its value.

For instance, I want to get "5555" from X-CUSTOMFIELD.

So far, my thoughts are:

"X-CUSTOMFIELD;\\d+"

I have been looking at some tutorials and I am a little confused with what function to use? What would my regex above return? Would it give me the whole line or just the numerical part (5555)?

I was thinking I i get the whole row, I can use substring to get the digits?

BEGIN:VCARD
VERSION:2.1
N:Last;First;
FN:First Last
TEL;HOME;VOICE:111111
TEL;MOBILE;VOICE:222222
X-CUSTOMFIELD;5555
END:VCARD
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
David
  • 3
  • 1
  • This question might be interesting to you: http://stackoverflow.com/questions/672704/where-to-find-a-java-library-to-read-vcard-files – David Webb May 26 '10 at 11:57

5 Answers5

0

you can search for the ";" or ":" and use the subString() method from the java API...as params you can add from where to where the substring shall be cutted.

als end-param you can use row.length-1, if "row" is your current line

poeschlorn
  • 12,230
  • 17
  • 54
  • 65
  • But I first need to isolate a row to use ":" or ";", right? Each row has one of those two punctuation characters. – David May 26 '10 at 11:22
  • jetz, that's right. when I remember it right, there is a method to read it line by line...something like "getLine()". – poeschlorn May 26 '10 at 11:34
  • I think that makes the assumption that the vcard is always in the same order? Also, there can be duplicate lines such as having more than one phone number or email address. – David May 26 '10 at 11:37
  • good question...I really don't know how fix this order is, I've never worked with that before...for this case you shouldn't concentrate on one fix number of rows but make it variable – poeschlorn May 26 '10 at 11:39
0

Why not try the following regex:

^([^:]+):(.*)$

What you'll have to do is read the vCard data line by line, then you will have two match groups (provided that the expression is correct)

Group 1 will be the field name (X-CUSTOMFIELD) and Group 2 will be the data of that field (5555)

You will have to do some monkeying around with that regex as it might not do exactly what you want but it will get you close to where you're looking to go.

Dave G
  • 9,639
  • 36
  • 41
  • My regex authoring is no way that advanced! Looks mind boggling. Kudos! I'm going to give SchlaWiener's suggestion a go first... – David May 26 '10 at 11:43
0

The format for vcard fields is KEY[;Attribute]:VALUE[;ATTRIBUTE]. It looks like a typo in X-CUSTOMFIELD and should read X-CUSTOMFIELD:5555

Then you can use line.split(":") on each line to get keys and values.

EDIT

First I'd read all lines from the file/message and store them as key/value paris in a Hashtable (HashMap is not available on android..). Use the split function I mentioned above to split the line into a key and a value part.

After this is done, simple ask the hashtable for the value for key "X-CUSTOMFIELD" and it should return "5555". (Assuming, you corrected the vcard format, I'm still positive that the X-CUSTOMFIELD line is invalid!)

EDIT 2

If vcard allows duplicate keys, then you still can use a hashtable as an internal vcard model but the value should be a List<String> type rather then a String and the vcard values are added to this list, like:

 Hashtable<String, List<String>> vcard = new Hashtable<String, List<String>>();
 for (String line:lines) { // assuming lines is an array or collection with all rows
   String keyValuePair = line.split(":");
   List<String> values = vcard.get(keyValuePair[0]);
   if (values == null) {
     // first value for this key - need to create the list
     values = new ArrayList<String>();
     vcard.put(keyValuePair[0], values);
   }
   values.add(keyValuePair[1]);
 }

(untested - if it doesn't compile, treat it as pseudo-code ;-) )

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • I do like the sound of this, Andreas. Although, there will be key duplications in case of telephone or email; I can have more than one TEL;HOME;VOICE ? I'll have to check on the custom field, as it would make sense for it to be colon like the others. :-) – David May 26 '10 at 11:56
0
            String customfield;
            Pattern p = Pattern.compile("X-CUSTOMFIELD;(.+)$");
            Matcher m = p.matcher(insertyourcomparestringhere);

            if (m.find()) customfield = m.group();

That should do the trick. Only 5555 is in the string customfield.

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • This looks promising, thank you. I'm going to give this a try... Geez, this is much harder than php! – David May 26 '10 at 11:37
  • I changed it to this: Pattern p = Pattern.compile("X-CUSTOMFIELD;\\d+"); and that seemed to do the trick! :-D Thank you!! – David May 26 '10 at 12:02
0
Pattern p = Pattern.compile("X-CUSTOMFIELD;(\\d+)");
Matcher m = p.matcher(your_string);
if(m.find()){
    MatchResult mr=m.toMatchResult();
    String value=mr.group(1);//this will be 5555
}
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Fedor
  • 43,261
  • 10
  • 79
  • 89