I'm trying to count how many entries in a piece of text conform to the
pattern $(Some text)$ or $(Something else)$. Essentially how many
peices of text are preceded by $( and followed by )$ .I'm using a
scanner and the regex found below, but I find nothing:
String PARAM_DELIMETER = "\\$\\([^\\(]+\\)\\$";
String Pattern PARAM_PATTERN;
PARAM_PATTERN = Pattern.compile(PARAM_DELIMETER);
Scanner scanner = new Scanner(theText);
String param;
while ( ( param = scanner.findInLine(PARAM_PATTERN) ) != null )
{
System.out.println("Found: "+param);
}
scanner.close();
However, when I do a replace thus it works:
theText.replaceAll("\\$\\(Some text\\)\\$", "REPLACEMENT");
Any ideas? Many thanks in advance.
Rhino - 09 May 2006 17:25 GMT
> I'm trying to count how many entries in a piece of text conform to the
> pattern $(Some text)$ or $(Something else)$. Essentially how many
[quoted text clipped - 20 lines]
>
> theText.replaceAll("\\$\\(Some text\\)\\$", "REPLACEMENT");
I'm not clear on whether you want to find pieces of text that literally
contain a $ sign, an open parenthesis, other characters, a close
parenthesis, and then another $ sign or whether you are using the $ symbol
in the same way the REGEX literature uses it, i.e. as a symbol that
represents the first character on the line that is being read.
If you are using the $ sign in the second sense, as a start-of-line symbol,
you should be aware that your pattern is unlikely to ever work since it uses
the $ sign _twice_ and expects to find characters between the first
character of the line and the first character of the line! Or at least
that's what I _think_ your expression means; I don't use Regular Expressions
very often and may be misremembering the syntax.
You should probably consult the REGEX articles that come with the JDK
documentation; there's a fairly good tutorial on how to construct various
REGEX patterns in that documentation. That's what I use every time I have to
write another REGEX pattern....
--
Rhino
Oliver Wong - 09 May 2006 18:13 GMT
> I'm trying to count how many entries in a piece of text conform to the
> pattern $(Some text)$ or $(Something else)$. Essentially how many
> peices of text are preceded by $( and followed by )$ .I'm using a
> scanner and the regex found below, but I find nothing:
>
> String PARAM_DELIMETER = "\\$\\([^\\(]+\\)\\$";
Perhaps you mean "\\$\\([%\\)]+\\)\\$"? That is, "One or more of
anything except for ')'" instead of "One or more of anything except for
'('".
- Oliver
JavaEnquirer - 10 May 2006 09:52 GMT
Hi, thanks for the reply. The revised expression didn't work either. To
clarify, I'm trying to do something like so:
Hello $(name)$, do you live in $(address)$
I'm trying to find out all of the text portions delimeted by $(....)$
i.e. scan through the text above and print out:
$(name)$
$(address)$
Jussi Piitulainen - 10 May 2006 10:44 GMT
> To clarify, I'm trying to do something like so:
>
[quoted text clipped - 5 lines]
> $(name)$
> $(address)$
Consider using a java.util.regex.Matcher instead of a Scanner. You get
a Matcher from the Pattern p by
p.matcher("Hello $(name)$, do you live in $(address)$")
and run through the matches with
while (m.find()) {
System.out.println(m.group(0));
}
if your pattern is "[$]\\([^()]+\\)[$]". See Matcher docs for more.
JavaEnquirer - 10 May 2006 11:30 GMT
Thanks for the reply, I'll try that too. Though, I have come up with a
solution/realised an error. I was using findInLine which terminates the
search when it reaches a new line character. I'm now using
findWithinHorizon passing the length of the text and that works fine.
The name findInLine should have been a give away really. However, given
that Scanner is recommended for this kind of task, I'd have expected a
more benign API with a method such as find() which simply calls
findWithinHorizon with the initially specified pattern and the length
of the initially specified string automatically provided. Ah, one day
I'm sure Sun will wake up and go : "Hmm, we've spent a man year on this
fancy bit of code, why not provide the odd convenience method that will
take 4.3 minutes to write."
Cheers again.