# Processing input file character wise

I want to process a non-English mixed file. For example, please see the following illustrative code:

\documentclass{article}
\begin{document}
{\it It is a statement in English.}
\par
{\bf This non-English line requires swapping.}
\end{document}


Now say, the texts within {\it } declaration does not require any change, but those within {\bf } declaration only. Here I need to put intermediate/ending 'e', 'i' and 'ui' prior to the immediate preceding consonant(s). That is, the statement in {\bf } should be printed as iThs non-Einglsh ilen eruiqers swaippng'. Is there any way to achieve it?

• it would be easy in luatex, possible in xetex if the rules onwhat to add are not too complicated, but for pdftex I would process with sed or perl before passing to tex. it isn't clear to me when you add e and when you add e – David Carlisle May 7 at 19:41
• Some "reegx exeprts" may be able to process a phrase as you wish but since It would likely require export and re-import (externalisation) it is best done as David suggests prior to tex then use a basic patch, import or include for compilation. – user170109 May 7 at 20:51
• If it is possible to modify the input to be the argument of a command (e.g., \processinput{This non-English line requires swapping} then you can use string processing packages for all engines, including pdflatex. – Marijn May 7 at 20:53

You can use the regex facilities of expl3:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\swap}{m}
{
\dilip_swap:n { #1 }
}

\tl_new:N \l__dilip_swap_tl

\cs_new_protected:Nn \dilip_swap:n
{
\tl_set:Nn \l__dilip_swap_tl { #1 }
\regex_replace_all:nnN
{ ([bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]*)(e|i|ui) }
{ \2 \1 }
\l__dilip_swap_tl
\tl_use:N \l__dilip_swap_tl
}
\ExplSyntaxOff

\begin{document}

\textit{It is a statement in English.}

\textbf{\swap{This non-English line requires swapping.}}

\end{document}


• Thanks a lot, you have done what I wanted. But I could not understand what you meant by 'regex facilities of expl3'. I got error messages upon compiling your example with both pdflatex and latex. – Dilip D. May 8 at 7:59
• @DilipD. You may need to update your TeX distribution. The expl3 language is a set of TeX programming facilities that also includes management of regular expressions. In the example above, any span of consonants followed by e, i or ui` is replaced by the vowel(s) preceding the consonant(s). – egreg May 8 at 8:37
• my TeXLive is up to date and your coding is also correct. The only thing required at my end was to load the {l3regex} package. But unfortunately I forgot to mention another point. Though the position of 'i' is to be changed, but not that of 'ii', i.e., if 'i' appears together twice, it will be treated as a single entry as 'ii' and hence its position will not be changed. Can you please add this provision in the coding? I have just now gone through the {l3regex} package, but could not understand how to do it. – Dilip D. May 9 at 4:52