Hemal Pandya wrote:
> You have to use string.length(). Just out of curiosity -- especially
> since you are not even using a Pattern but directly using
> String.matches
I am using a Pattern indirectly.
"An invocation of this method of the form str.matches(regex) yields
exactly the same result as the expression
Pattern.matches(regex, str)"
-- what syntax (API) would you want to use to check the
> total length using regexp?
I don't grasp your question.
Just to clarify: I wanted to know whether regexp can check for the total
length of the string but as you mentioned, I have to use
String.length(). Hence I conclude that regexp cannot be used to check
the length. So I think the answer to your question is 'no syntax'.
Hemal Pandya - 15 Jul 2005 08:07 GMT
> Hemal Pandya wrote:
>
[quoted text clipped - 3 lines]
>
> I am using a Pattern indirectly.
Sorry. I meant to say you are not using Pattern _directly_. Since you
are using the String method, which checks if the /entire/ string
matches the pattern (and not that it contains a substring that matches
the pattern) I do not understand why you don't want to use the String
method.
> "An invocation of this method of the form str.matches(regex) yields
> exactly the same result as the expression
[quoted text clipped - 5 lines]
>
> I don't grasp your question.
/If/ it was possible to use regexp to check whether a string has a
total
length of x characters ; what API would you expect in regexp for this
purpose?
If you expect that method to be in String then String.length is that
method :-)
If you expect that method to be in Pattern or in Matcher, I would argue
that it does not belong there.
- - 15 Jul 2005 10:27 GMT
Hemal Pandya wrote:
> /If/ it was possible to use regexp to check whether a string has a
> total
[quoted text clipped - 5 lines]
> If you expect that method to be in Pattern or in Matcher, I would argue
> that it does not belong there.
It is possible to use regexp to check whether a string has a total of x
characters but my question pertains to a string which contains several
tokens and that each token can be of varying length.
A simple example would be:
[A] B [C]
where [A] and [C] are optional tokens and A, B, and C are strings of
varying length.
I can have several forms:
aa bbb
aaa b ccc
bbbbbb c
aaaaaaaa bbbbbbbbbb ccccccccc
so on and so forth.
What if I only want the total length to be less than x? Is there a
regexp construct that does that?
Hemal Pandya - 15 Jul 2005 17:05 GMT
> What if I only want the total length to be less than x? Is there a
> regexp construct that does that?
Not that I know of.
Matthias Buelow - 01 Aug 2005 00:38 GMT
>What if I only want the total length to be less than x? Is there a
>regexp construct that does that?
If you want to check for a string of length <= n, you have to check
for n optional characters. Either you write it out, or you use a
bound, if your regexp implementation allows that (see docs). But what
do you want to achieve with that? Surely checking for string length is
a lot faster than having the regex engine construct a finite automaton
from the expression and traverse that (which is what most regex
packages do, in some way or other.)
mkb.