Re: Need help with regular expression
- From: Lars Enderin <lars.enderin@xxxxxxxxx>
- Date: Wed, 28 Feb 2007 13:14:26 GMT
Hendrik Maryns skrev:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Lars Enderin schreef:Lars Enderin skrev:heAzk skrev:Remove the extra ": The pattern should be "<-- %%(.*?)%% -->".Luc Van Bogaert wrote:I don't think Luc implied that there can be at most three charactersHi,The '.*' is used to match zero or more occurances of any character.
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 '*' 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}'.
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("<"-- %%(.*?)%% -->");
Almost: "<!-- %%(.*?)%% -->"; but I do not really see why you would need
the parens, "<!-- %%.*?%% -->" should suffice.
The parentheses are not *necessary*, but useful if you want to know what the special comment contains.
(Your signature should start with a line containing only "-- " (two dashes and a single space). This will enable news/mail clients to recognize it as a signature and, for example, remove it automatically from quoted material in replies.)
.
- Follow-Ups:
- Re: Need help with regular expression
- From: Hendrik Maryns
- 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
- From: Hendrik Maryns
- Re: Need help with regular expression
- Prev by Date: Re: Need help with regular expression
- Next by Date: Error loading SolarisSerial
- Previous by thread: Re: Need help with regular expression
- Next by thread: Re: Need help with regular expression
- Index(es):
Relevant Pages
|