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 / General / July 2005

Tip: Looking for answers? Try searching our database.

Using regexp to check for length of string that accepts optional strings

Thread view: 
- - 15 Jul 2005 03:31 GMT
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) {
    ...
}
Hemal  Pandya - 15 Jul 2005 04:37 GMT
> 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
[quoted text clipped - 9 lines]
>      ...
> }

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?
- - 15 Jul 2005 05:51 GMT
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.
hiwa - 15 Jul 2005 07:20 GMT
> 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
[quoted text clipped - 9 lines]
>      ...
> }
Quantifiers may or may not do you the service. Read the javadoc for
java.util.regex.Pattern class.
- - 15 Jul 2005 09:08 GMT
>>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
[quoted text clipped - 12 lines]
> 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.


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.