Mixing Switch and RegExMatch() Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Ross
Posts: 100
Joined: 13 Mar 2016, 00:27

Mixing Switch and RegExMatch()

27 Nov 2021, 10:13

Hello, I need to process a vcard contacts list in a particular way. The vcard file is a plain text file as follows:

Code: Select all

BEGIN:VCARD
VERSION:3.0
FN:Person 1's Full name
N:Last Name;First Name;;;
TEL;TYPE=CELL:01234567890123
NOTE:friend
CATEGORIES:myContacts
END:VCARD

BEGIN:VCARD
VERSION:3.0
FN:Person 2's Full name
N:Last Name;First Name;;;
TEL;TYPE=CELL:98765432109876
TEL;TYPE=HOME:8765432109876
CATEGORIES:myContacts
END:VCARD

The idea is to use a Loop and a FileReadLine to check the form of each line. If it matches BEGIN:VCARD, then I know it has reached a new entry (so I can increment a count, for example); if it matches the regex ^FN:.+, then I'll know it's the person's full name; if it matches the regex ^TEL;TYPE=\w{4}:.+, I'll know it's the phone number.

So I thought about using Switch:

Code: Select all

contact_count := 0

Loop, {
FileReadLine, line, contacts.vcf, A_Index

Switch line
{
Case "BEGIN:VCARD":
    MsgBox, New contact begins here
    contact_count += 1
Default:
    MsgBox, Didn't find anything fancy on this line.
}
[...]
As you can see, if the line is a exact, literal match to BEGIN:VCARD, then I'm able to perform some actions just below. BUT, when it comes to the First Name line (the one beginning in FN:), the line's contents will vary, so I would need to use a RegexMatch(). But how to mix the two? It's not like this, for instance:

Code: Select all

contact_count := 0

Loop, {
FileReadLine, line, contacts.vcf, A_Index

Switch line
{
Case "BEGIN:VCARD":
    MsgBox, New contact begins here.
    contact_count += 1
Case "RegexMatch(line, `"(?<=FN:)(.+)$`", out_var)":
    MsgBox, This is the contact's name.
    contact_count += 1
Default:
    MsgBox, Didn't find anything fancy on this line.
}
[...]
How do I do that? ;)
User avatar
mikeyww
Posts: 26936
Joined: 09 Sep 2014, 18:38

Re: Mixing Switch and RegExMatch()  Topic is solved

27 Nov 2021, 10:16

Use Switch on a line by itself. Each case can then be a conditional statement. The first match wins.

Code: Select all

Switch
{ Case 1 = 3: Send x
  Case 1 = 1: Send y
}
Alternatively:

Code: Select all

line = FN:abc
RegExMatch(line, "^(.+?):(.+)", field)
Switch field1
{ Case "BEGIN": Send x
  Case "FN"   : Send %field2%
}
User avatar
Ross
Posts: 100
Joined: 13 Mar 2016, 00:27

Re: Mixing Switch and RegExMatch()

27 Nov 2021, 19:44

Thanks a lot @mikeyww, you helped! My script ended up like this:

Code: Select all

#SingleInstance, Force
SendMode Input
SetWorkingDir, %A_ScriptDir%

count     := 0
tel_count := 0
names_numbers := "C:\Users\me\Desktop\contacts and their numbers.txt"

Loop, {
FileReadLine, line, contacts.vcf, A_Index

    Switch
    {
    Case RegExMatch(line, "BEGIN:VCARD") > 0: 
        count += 1
        tel_count := 0
        name := ""
    Case RegExMatch(line, "^FN:.+"): 
        name := RegExReplace(line, "^FN:", "")
        FileAppend, %name%;, %names_numbers%
    Case RegExMatch(line, "^TEL;TYPE=\w{4}"): 
        tel := RegExReplace(line, "^TEL;TYPE=\w{4}:", "")
        If tel_count = 0 
        {
            FileAppend, %tel%`n, %names_numbers%
            tel_count += 1
        }

        Else 
        {
            FileAppend, %name%;%tel%`n, %names_numbers%
        }
    Case RegExMatch(line, "^item\d.TEL:"): 
        tel := RegExReplace(line, "^item\d.TEL:", "")
        If tel_count = 0 
        {
            FileAppend, %tel%`n, %names_numbers%
            tel_count += 1
        }

        Else 
        {
            FileAppend, %name%;%tel%`n, %names_numbers%
        }
    
    Default: 
        ; MsgBox, Nothing expected.
    }
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 145 guests