Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2007

Tip: Looking for answers? Try searching our database.

Need help with regular expression

Thread view: 
Luc Van Bogaert - 26 Feb 2007 11:16 GMT
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,
Signature

Luc Van Bogaert

heAzk - 26 Feb 2007 11:33 GMT
> Hi,
>
[quoted text clipped - 15 lines]
>
> 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}'.

-heAzk
Lars Enderin - 26 Feb 2007 12:02 GMT
heAzk skrev:
>> Hi,
>>
[quoted text clipped - 20 lines]
> 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("<"-- %%(.*?)%% -->");

...
Matcher m;
...
For each line:
m = commentPtrn.matcher(line);
if (m.find()) {
  // The pattern exists, and the string between the %%-s is m.group(1).
}

See class Pattern and class Matcher in java.util.regex.
Lars Enderin - 26 Feb 2007 12:24 GMT
Lars Enderin skrev:
> heAzk skrev:
>>> Hi,
[quoted text clipped - 28 lines]
>
> static Pattern commentPtrn = Pattern.compile("<"-- %%(.*?)%% -->");

Remove the extra ": The pattern should be "<-- %%(.*?)%% -->".

> ...
> Matcher m;
[quoted text clipped - 6 lines]
>
> See class Pattern and class Matcher in java.util.regex.
Luc Van Bogaert - 27 Feb 2007 09:09 GMT
> Remove the extra ": The pattern should be "<-- %%(.*?)%% -->".
> >
[quoted text clipped - 8 lines]
> >
> > See class Pattern and class Matcher in java.util.regex.

Thanks for your help.

Signature

Luc Van Bogaert

Hendrik Maryns - 28 Feb 2007 11:00 GMT
Lars Enderin schreef:
> Lars Enderin skrev:
>> heAzk skrev:
[quoted text clipped - 31 lines]
>
> 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
Lars Enderin - 28 Feb 2007 13:14 GMT
Hendrik Maryns skrev:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 36 lines]
> 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.)
Hendrik Maryns - 28 Feb 2007 15:14 GMT
Lars Enderin schreef:
> Hendrik Maryns skrev:
>> -----BEGIN PGP SIGNED MESSAGE-----
[quoted text clipped - 41 lines]
> The parentheses are not *necessary*, but useful if you want to know what
> the special comment contains.

/I/ know that, but I wanted to point it out to the OP.

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

(I know, it has to do with Enigmail and Thunderbird messing up PGP
signed messages.  There is a bug for it somewhere.)

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.