Re: Need help with regular expression
- From: Hendrik Maryns <hendrik_maryns@xxxxxxxxxxxxx>
- Date: Wed, 28 Feb 2007 12:00:25 +0100
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Lars Enderin schreef:
Lars Enderin skrev:
heAzk skrev:
Luc Van Bogaert wrote:
Hi,
I'm trying to construct a regular expression to check the occurance of
two substrings in a String, but I haven't yet found something that
works. Could someone please help?
This is used to parse lines of HTML code.
I'm using String.matches(regex) to find out if the string matches
anything like this :
...<!-- %%...%% -->...
The dots can be replaced with anything or even nothing.
This doesn't seem to work :
String.matches(".*" + "<!-- %%" + ".*" + "%% -->"+ ".*")
Thanks,
The '.*' is used to match zero or more occurances of any character.
The '*' modifier is greedy however. This means that it'll match as
many times as it can. You need to match only zero to three times,
which can be accomplished by '.{lower, upper}', so in your case:
'.{0,3}'.
I don't think Luc implied that there can be at most three characters
between the %%-s, and I don't think that String.matches(regex) is the
right method to use. It would be better to use something like
static Pattern commentPtrn = Pattern.compile("<"-- %%(.*?)%% -->");
Remove the extra ": The pattern should be "<-- %%(.*?)%% -->".
Almost: "<!-- %%(.*?)%% -->"; but I do not really see why you would need
the parens, "<!-- %%.*?%% -->" should suffice.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFF5WDJe+7xMGD3itQRAgktAJ98oih/3RPNoJU1wiRNQOiXeNU+DQCffPfy
W95deVqAqIM1WVbxrzoi4FM=
=VxVY
-----END PGP SIGNATURE-----
.
- Follow-Ups:
- Re: Need help with regular expression
- From: Lars Enderin
- Re: Need help with regular expression
- References:
- Re: Need help with regular expression
- From: heAzk
- Re: Need help with regular expression
- From: Lars Enderin
- Re: Need help with regular expression
- From: Lars Enderin
- Re: Need help with regular expression
- Prev by Date: Re: Java Applet Notinited
- Next by Date: Re: Need help with regular expression
- Previous by thread: Re: Need help with regular expression
- Next by thread: Re: Need help with regular expression
- Index(es):
Relevant Pages
|