Check if String matches one of multiple options?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
RNIB
Macro Veteran
Posts: 195
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Check if String matches one of multiple options?

Post by RNIB » Fri Nov 08, 2024 3:14 pm

I'm reading a line of metadata within a HTML file to get the language code. The line will appear as something like:
<meta name="dc:language" content="en-GB"/>
This is captured as strDCLANGValue1 earlier in my code

However, the only bit I'm interested in is the content between " ", in this case en-GB. So I'm stripping everything else away with the following.

Code: Select all

LTrim>strDCLANGValue1,strDCLANGValue1
Let>dcLangpattern=dc:language.+?content="\K[^"]+
RegEx>dcLangpattern,strDCLANGValue1,0,dcLANGMatches1,nm,0
Trim>dcLANGMatches1_1,dcLANGMatches1_1
So in the above example, dcLANGMatches1_1=en-GB

However, the language code could be one of 140 different options. How can I check if the code entered does match one of the 140 different options?

The language codes are (they are not case sensitive):
ab
om
aa
af
sq
am
ar
hy
as
ay
az
ba
eu
bn
dz
bh
bi
br
bg
my
be
km
ca
zh
co
hr
cs
da
nl
en
en-GB
en-US
eo
et
fj
fi
fr
fy
gl
ka
de
el
kl
gn
gu
ha
he
hi
hu
is
id
ia
ie
iu
ik
ga
it
ja
jv
kn
ks
kk
rw
ky
rn
ko
ku
lo
la
lv
ln
lt
mk
mg
ms
ml
mt
mi
mr
mo
mn
na
ne
no
oc
or
ps
fa
pl
pt
pa
qu
rm
ro
ru
sm
sg
sa
gd
sr
sh
st
tn
sn
sd
si
ss
sk
sl
so
es
su
sw
sv
tl
tg
ta
tt
te
th
bo
ti
to
ts
tr
tk
tw
ug
uk
ur
uz
vi
vo
cy
wo
xh
yi
yo
za
zu

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Check if String matches one of multiple options?

Post by JRL » Fri Nov 08, 2024 5:23 pm

I would put the list to a variable then Separate> the list using your string parsed result variable. Also if there is a chance for string case to fluctuate, make sure to assign case to avoid errors.

Code: Select all

LabelToVar>LanguageCode,vLanguageCodeList
UpperCase>vLanguageCodeList,vLanguageCodeList

Let>dcLANGMatches1_1=en-GB
UpperCase>dcLANGMatches1_1,dcLANGMatches1_1

Separate>vLanguageCodeList,dcLANGMatches1_1,vLanguageCodeTest
If>vLanguageCodeTest_Count>1
  MDL>%dcLANGMatches1_1% is in the list
Else
  MDL>%dcLANGMatches1_1% is not in the list
EndIf


/*
LanguageCode:
ab
om
aa
af
sq
am
ar
hy
as
ay
az
ba
eu
bn
dz
bh
bi
br
bg
my
be
km
ca
zh
co
hr
cs
da
nl
en
en-GB
en-US
eo
et
fj
fi
fr
fy
gl
ka
de
el
kl
gn
gu
ha
he
hi
hu
is
id
ia
ie
iu
ik
ga
it
ja
jv
kn
ks
kk
rw
ky
rn
ko
ku
lo
la
lv
ln
lt
mk
mg
ms
ml
mt
mi
mr
mo
mn
na
ne
no
oc
or
ps
fa
pl
pt
pa
qu
rm
ro
ru
sm
sg
sa
gd
sr
sh
st
tn
sn
sd
si
ss
sk
sl
so
es
su
sw
sv
tl
tg
ta
tt
te
th
bo
ti
to
ts
tr
tk
tw
ug
uk
ur
uz
vi
vo
cy
wo
xh
yi
yo
za
zu
*/

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1389
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Check if String matches one of multiple options?

Post by Dorian (MJT support) » Fri Nov 08, 2024 5:37 pm

That's a remarkably clever way of using Separate. I just used Position. Also that's a good point re the case, which I hadn't thought of, so added it. Here's what I had :

Code: Select all

Let>SearchString=en-GB
UpperCase>SearchString,SearchString

LabelToVar>codes,codelist
UpperCase>codelist,codelist

Position>Searchstring,codelist,1,PosSearch,

If>PosSearch>0
  MDL>Is in list
  else
  MDL>Isn't in list
Endif

/*
codes:
ab
om
aa
af
sq
am
ar
hy
as
ay
az
ba
eu
bn
dz
bh
bi
br
bg
my
be
km
ca
zh
co
hr
cs
da
nl
en
en-GB
en-US
eo
et
fj
fi
fr
fy
gl
ka
de
el
kl
gn
gu
ha
he
hi
hu
is
id
ia
ie
iu
ik
ga
it
ja
jv
kn
ks
kk
rw
ky
rn
ko
ku
lo
la
lv
ln
lt
mk
mg
ms
ml
mt
mi
mr
mo
mn
na
ne
no
oc
or
ps
fa
pl
pt
pa
qu
rm
ro
ru
sm
sg
sa
gd
sr
sh
st
tn
sn
sd
si
ss
sk
sl
so
es
su
sw
sv
tl
tg
ta
tt
te
th
bo
ti
to
ts
tr
tk
tw
ug
uk
ur
uz
vi
vo
cy
wo
xh
yi
yo
za
zu
*/
Yes, we have a Custom Scripting Service. Message me or go here

RNIB
Macro Veteran
Posts: 195
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Re: Check if String matches one of multiple options?

Post by RNIB » Mon Nov 11, 2024 11:00 am

This is brilliant. Thank you both so much. I had previously been playing around with using separate but just couldn't get my head around how to make it work and it never occurred to me to use Position. Both solutions are far, far more elegant than anything I was crudely trying to create and work beautifully.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts