Re: Need help with regular expression



-----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-----
.



Relevant Pages

  • Re: Find the n-th occurance of a sustring within a string
    ... You use a character known never to appear in the data, such as the pipe character in this example. ... This unlikely string occurs only when Sarge is swearing. ... Suppose the string you want to search is in A1, the substring you want to find is in A2 and the substring instance you want to find is in A3, then the following formula will return the position where the nth occurrence of the substring being searched for starts: ... Lets say you wanted to find the 10th occurance of a particular string within a string, now the formula gets really hairy, if you nest. ...
    (microsoft.public.excel)
  • strchr & finding multiple occurances of a char
    ... search for multiple occurances of a same character in a string. ... successful condition. ... if I want to parse down the string and look for each occurance ...
    (comp.lang.c)
  • Re: Find the n-th occurance of a sustring within a string
    ... It can be fooled, however, in the unlikely event any instance of the string ... You use a character known never to appear in the data, such as the pipe character in this ... Suppose the string you want to search is in A1, the substring you want to find is in A2 ... Any clever ideas out there to find the n-th occurance of a string within another? ...
    (microsoft.public.excel)
  • Count occurance of a character in a string.
    ... occurance of a character in a given string. ... Thansk ...
    (microsoft.public.access.modulesdaovba)
  • Re: find last occurance of a character in a string
    ... should be able to use the VBA function InStrRev. ... > I need a formula or expression that will tell me the index of last occurance ... > of a character in a string. ...
    (microsoft.public.access.queries)