# regular expression in listings

I'm looking for a possibility to find a way to colorize data. If a text starts in a line with XYZ from that point until the next blank line the text should be set in a defined colour. With the comment feature like

comment=[s]{^XYZ}{

}


it is not possible.

comment=[s]{XYZ}{</end>}


works, but it is not solution for my need. At least, XYZ can also be in the normal text, but only if a line starts with it, the text block should be coloured. Is there a solution with listings?

Here is an example:

\documentclass{report}
\usepackage{listings,xcolor}
\lstdefinelanguage{text}{}
\lstnewenvironment{mylang}{\lstset{language=text,
comment=[s]{XYZ}{</end>},%
basicstyle=\footnotesize\ttfamily,%
showstringspaces=false,%
showspaces=false,%
}}{}

\begin{document}
\begin{mylang}
Only a test, this is not a comment
XYZ this should colored as a comment
and this line too

this is not a command, because only lines starting with XYZ

This is not a comment, too
\end{mylang}
\end{document}


Here is an example:

\documentclass{report}
\usepackage{listings,xcolor}
\lstdefinelanguage{text}{}
\lstnewenvironment{mylang}{\lstset{language=text,
comment=[s]{XYZ}{</end>},%
basicstyle=\footnotesize\ttfamily,%
showstringspaces=false,%
showspaces=false,%
}}{}

\begin{document}
\begin{mylang}
Only a test, this is not a comment
XYZ this should colored as a comment
and this line too

this is not a command, because only lines starting with XYZ

You have to change the \catcode of the newline character to other before defining the environment. Afterwards, you probably want to change the old meaning back:) The following code does the trick. It looks ugly, because everything is on one line, but it has to be, since newline is not newline in here! By the way, if you prefer, you can replace ^^Ms with actual newlines.
{\catcode\^^M=12 \def\marshal{\lstnewenvironment{mylang}{\lstset{language=text,comment=[s]{^^MXYZ}{^^M^^M},commentstyle=\color{green},basicstyle=\footnotesize\ttfamily,showstringspaces=false,showspaces=false,}}{}}\expandafter}\marshal
`