# Dynamically applying formatting to regular expressions [closed]

One useful tool I find in InDesign are the Grep styles. With them, one can define character styles (e.g., boldface in small caps) for whatever expression matches the grep pattern.

I know this is quite easy to statically carry this out with the help of most modern text editors — viz., searching for a regex pattern and replacing all matches with the desired code (e.g., `{\bfseries\scshape. . .}` or whatever).

But can this be done dynamically in LaTeX?

For instance, I would like to tell LaTeX something like the following:

``````\newregexstyle{^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\$}{\bfseries\scshape}
``````

so that all matching expressions (in this case, syntactically valid email addresses) are dynamically formatted in the desired manner. (Yes, I know — there would be a problem here with chars `%`, `@`, `\$` and the like. But this is just an example. The syntax should conform to `l3regex`'s rules.)

The good thing about dynamically sorting this out is (1) that the coded text looks cleaner (and easier to edit), (2) that when writing the text I should care not for these expressions' styles, and (3) that I can easily change (or even remove) the format at any time without much editing. (I'm thinking here of complex documents with many `\include` and `\input` commands.)

-
I don't agree with your assertion (1): `\email{x@y.z}` is the way to go in TeX and what makes for cleaner code. –  egreg Oct 7 '12 at 21:45
`x@y.z` seems to me cleaner than `\email{x@y.z}`, just as `\email{...}` does in comparison with `{\bfseries\scshape...}`. Be that as it may, I think my question is still worth considering. –  NVaughan Oct 7 '12 at 21:54
Consider also that the email address can be produced by a macro. A (La)TeX document is not only text, but also mark up. –  egreg Oct 7 '12 at 21:57
PS. I'm thinking in cleanliness not just as a coder, but also as a (copy)editor who works with LaTeX. When copyediting text, it's easier for me to concentrate on the text and leave aside (some of) the formatting. –  NVaughan Oct 7 '12 at 21:58
Yes, you're right about that. That means that macros should (might) be expanded before regex styles could be applied. –  NVaughan Oct 7 '12 at 22:00