# [OS X TeX] ifthen and regular expressions

Jeffrey J Weimer weimerj at email.uah.edu
Thu May 24 11:26:21 EDT 2007

On May 23, 2007, at 2:59 PM, Friedrich Vosberg wrote:

> Am 15.05.2007 um 17.11 schrieb Jeffrey J Weimer:
>>
>> On May 15, 2007, at 7:13 AM, Friedrich Vosberg wrote:
>>
>>> Does someone of You know how to get an ifthenelse switch that is
>>> TRUE if the expansion of the given macro contains a given string.
>>> For example:
>>>
>>> \def\string{court}
>>> \def\pattern{Royal High Court of Westminster}
>>>
>>> \ifthenelse{contains{\string}{\pattern}{%
>>>   TRUE
>>> }{%
>>>   FALSE
>>> }
>>
>> The coolstr package may be able to handle this.

I think a better way to do this is to split the pattern of words and
match each word separately (again through a loop) using a simple
\ifthenelse{\equal{word#N}{matchword}}{TRUE}{FALSE}. If the pattern
always had the same number of words (say 5), then this might work ...

\def\FindStrInStr#1<#2 #3 #4 #5 #6>{
\ifthenelse{\equal{#1}{#2}}{true}
{\ifthenelse{\equal{#1}{#3}}{true}
{\ifthenelse{\equal{#1}{#4}}{true}
{\ifthenelse{\equal{#1}{#5}}{true}
{\ifthenelse{\equal{#1}{#6}}{true}{false}
}
}
}
}
}

\ifthenelse{\FindStrInStr{Court}<Royal High Court of Westminster>}{do
if true}{do if false}

Putting this into a loop that would parse out each word of the
pattern and pass it to be tested would be the next step. I have tried
to do this, however I have run into the limitation that definitions
parse input arguments as characters. Below is the beginning part of
what I have set up so far. I cannot figure out how to test whether
the argument passed to \fv at nextstep is the LAST word of the string ...

\documentclass{article}
\usepackage{xifthen}
\makeatletter
\def\fv at nextstep#1 #2{almost true (#1)(#2)}
\def\fv at checkstrmatch<#1>#2 #3{%
\ifthenelse{\equal{#1}{#2}}%
{true}%
{\ifthenelse{\equal{#3}{}}%
{false}%
{%
\fv at nextstep#3%
}%
}%
}
\def\FindStrInStr#1#2{\fv at checkstrmatch<#1>#2}
\makeatother

\begin{document}

Here is a test to try men's souls ...

\FindStrInStr{Court}{High Court}

\end{document}

--
J. J. Weimer, Chemistry / Chemical & Materials Engineering
University of Alabama in Huntsville, MSB 125, 301 Sparkman Dr
Huntsville, AL 35899                phone: 256-824-6954