# Highlighting strings between [ and ] in lstlistings

I want to use lstlisting to present a file like this:

#comment
[keyword]
key1=value1
key2=value with RegEx

[keyword:NewKeyword]
key1 = value1
key2=value with RegEx


and this is my language definition:

\lstdefinelanguage{myLang}{
sensitive=false,
morestring=[b]",
keywords={=},
morecomment=[l]{\#},
}


How can I extract [keyword] and [keyword:NewKeyword]?

-
Note that "highlighting" and "extracting" can mean two completely different things. I'm assuming you mean the former - highlighting. –  Werner Nov 17 '11 at 15:20
–  Peter Grill Nov 17 '11 at 15:39

I you want to highlight the words with the [...], you can use moredelim (as with myLangStyleA) which discards the delimiters and applies the given style. If you want to keep the brackets you can use morecomment (as with myLangStyleB)

\documentclass[border=2pt]{standalone}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{filecontents}% only need to provide the file foo.mylang

\begin{filecontents*}{foo.mylang}% provide file foo.mylang
#comment
[keyword]
key1=value1
key2=value with RegEx

[keyword:NewKeyword]
key1 = value1
key2=value with RegEx
\end{filecontents*}

\lstdefinelanguage{myLang}{
sensitive=false,
morestring=[b]",
keywords={=},
morecomment=[l]{\#},
}

\lstdefinestyle{myLangStyleA}{
language=myLang,
moredelim=[is][\color{blue}\ttfamily]{[}{]}
}

\lstdefinestyle{myLangStyleB}{
language=myLang,
morecomment=[s][\color{red}\ttfamily]{[}{]},
}

\begin{document}
Using \textbf{style=myLangStyleA}:
\lstinputlisting[style=myLangStyleA]{foo.mylang}

Using \textbf{style=myLangStyleB}:
\lstinputlisting[style=myLangStyleB]{foo.mylang}
\end{document}

-
I think the OP wants to keep the brackets: the settings seems that of configuration files. –  egreg Nov 17 '11 at 15:41
@egreg: Ok. Have updated the solution that provides both options. –  Peter Grill Nov 17 '11 at 15:56