开发者

Getting specific values with regex

开发者 https://www.devze.com 2022-12-31 21:51 出处:网络
I need to knowingly isolate each row of the vCard and get its value. For instance, I want to get \"5555\" from X-CUSTOMFIELD.

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 o开发者_运维技巧r 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


            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.


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


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.


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 ;-) )


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消