Using regexp to check for length of string that accepts optionalstrings

?

-

Is it possible to use regexp to check whether a string has a total
length of x characters if the string accepts optional parameters for e.g

[OptionalPrefix] VALUE [OptionalPostfix]

where each of the token can be of any length but the total length must
be less than x?

Is it possible or do I have to use:

if (string.matches(regexp) && string.length() < x) {
...
}
 
H

Hemal Pandya

- said:
Is it possible to use regexp to check whether a string has a total
length of x characters if the string accepts optional parameters for e.g

[OptionalPrefix] VALUE [OptionalPostfix]

where each of the token can be of any length but the total length must
be less than x?

Is it possible or do I have to use:

if (string.matches(regexp) && string.length() < x) {
...
}

You have to use string.length(). Just out of curiosity -- especially
since you are not even using a Pattern but directly using
String.matches -- what syntax (API) would you want to use to check the
total length using regexp?
 
?

-

Hemal said:
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'.
 
H

hiwa

- said:
Is it possible to use regexp to check whether a string has a total
length of x characters if the string accepts optional parameters for e.g

[OptionalPrefix] VALUE [OptionalPostfix]

where each of the token can be of any length but the total length must
be less than x?

Is it possible or do I have to use:

if (string.matches(regexp) && string.length() < x) {
...
}
Quantifiers may or may not do you the service. Read the javadoc for
java.util.regex.Pattern class.
 
H

Hemal Pandya

- said:
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

Pattern.matches(regex, str)"

-- what syntax (API) would you want to use to check the

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

-

hiwa said:
- said:
Is it possible to use regexp to check whether a string has a total
length of x characters if the string accepts optional parameters for e.g

[OptionalPrefix] VALUE [OptionalPostfix]

where each of the token can be of any length but the total length must
be less than x?

Is it possible or do I have to use:

if (string.matches(regexp) && string.length() < x) {
...
}

Quantifiers may or may not do you the service. Read the javadoc for
java.util.regex.Pattern class.

I'm already using Pattern (albeit indirectly) for the String.matches'
regexp and I don't think there is a syntax to check the "dynamic" length.
 
?

-

Hemal said:
/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.

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?
 
M

Matthias Buelow

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top