Re: Need help with regular expression



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

-heAzk
.



Relevant Pages

  • Re: Need help with regular expression
    ... 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 ... I'm using String.matches(regex) to find out if the string matches ...
    (comp.lang.java.help)
  • Can they be expressed as regular expression?
    ... Given a string, and a regular expression, how many times do the regular ... know whether I could just use Finite automata to solve the above problem. ... possible for "less then or equal to k substrings" and "greater then or equal ...
    (comp.theory)
  • Re: regular expression all string but not .dwg
    ... Technically, a regular expression will either accept a given input string, or reject it. ... When searching with a regular expression is done, what usually happens is that a whole bunch of substrings of the so-called "haystack string" are passed over to the regular expression, and the longest such substring is returned as the so-called "needle string". ... So for example, you could say "Give me all non-overlapping substrings of parameter1 which do not contain '.dwg'", and then pass ".dwg" as parameter1, and the search engine will ...
    (comp.lang.java.programmer)
  • Re: How to write Regular Expression for recursive matching?
    ... lisong wrote: ... I have problem to split a string like this: ... and I want to get all substrings with only one '.' ... is there a way to get 'defg.hij' using regular expression? ...
    (comp.lang.python)
  • Re: Regular Expression for Zipcode parsing
    ... >> I want a regular expression that will match the last occurance ... > trailing whitespace that might be in the string. ... that will match only 9-digit zips. ...
    (comp.lang.perl.misc)