Question

I'm not very familiar with Regex so I'm asking it here.

I've a string like this:

BEGIN:VCARD
VERSION:3.0
FN:A Sirius Swimwear Collection
N:Collection;A;Sirius Swimwear;;
TEL;TYPE=HOME:22448
TEL;TYPE=CELL:52316
ADR:;Taylors Road;;;;;
ORG:Norfolk Phone Book
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:A Walk in the Wild
N:Wild;A;Walk in the;;
TEL;TYPE=HOME:22502
item1.TEL:23205
item1.X-ABLabel:Facsimile
ADR:;Grassy Road;;;;;
ORG:Norfolk Phone Book
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:AATA Orn Tours
N:Tours;AATA;Orn;;
TEL;TYPE=HOME:23611
TEL;TYPE=CELL:50755
ADR:;Bookings;;;;;
ORG:Norfolk Phone Book
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:Aatuti Art
N:Art;Aatuti;;;
TEL;TYPE=HOME:23669
ADR:;The Village;;;;;
ORG:Norfolk Phone Book
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:ABC Hobby Centre
N:Centre;ABC;Hobby;;
TEL;TYPE=HOME:22139
ADR:;Taylors Road;;;;;
ORG:Norfolk Phone Book
END:VCARD
(...)

For those familiar with, it is a bunch of VCards and I want to get a VCard one by one from the String. So, I want to split it up in multiple Strings. I want to split with the BEGIN:VCARD and END:VCARD tags

End up with:

vcard1 = "BEGIN:VCARD
    VERSION:3.0
    FN:A Sirius Swimwear Collection
    N:Collection;A;Sirius Swimwear;;
    TEL;TYPE=HOME:22448
    TEL;TYPE=CELL:52316
    ADR:;Taylors Road;;;;;
    ORG:Norfolk Phone Book
    END:VCARD";

vcard2 = "BEGIN:VCARD
    VERSION:3.0
    FN:A Walk in the Wild
    N:Wild;A;Walk in the;;
    TEL;TYPE=HOME:22502
    item1.TEL:23205
    item1.X-ABLabel:Facsimile
    ADR:;Grassy Road;;;;;
    ORG:Norfolk Phone Book
    END:VCARD";

vcard3 = "BEGIN:VCARD
    VERSION:3.0
    FN:AATA Orn Tours
    N:Tours;AATA;Orn;;
    TEL;TYPE=HOME:23611
    TEL;TYPE=CELL:50755
    ADR:;Bookings;;;;;
    ORG:Norfolk Phone Book
    END:VCARD";

Honestly, I just need the right regex pattern as the cycles I'm able to do them. This is in JAVA (For Android).

EDIT

This is what I've tried so far to no success:

if(vcard.contains("VERSION:"))
        {
            Log.d(TAG, "Creating the patterns");
            Pattern p = Pattern.compile("BEGIN:VCARD(.*)END:VCARD");

            Matcher m = p.matcher(vcard);

            if (m.find()) 
            {

                Log.d(TAG, "Find: " + m.group(1));

            }
            Log.d(TAG, "Not found anything");
Was it helpful?

Solution

You can use Pattern.DOTALL with your regex. It means In dotall mode, the expression dot(.) matches any character, including a line terminator and it will help you to match pattern from multiple lines.

String input = "__YOUR__VCARDS__";
Pattern pattern = Pattern.compile("(BEGIN:VCARD.*?END:VCARD)", Pattern.DOTALL);
Matcher m = pattern.matcher(input);
while (m.find()) {
    System.out.println(m.group(1));
}

OTHER TIPS

You should try ez-vcard. It's a Java vCard library that works on Android.

Disclaimer: I am the author ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top