Using regexp to check for length of string that accepts optionalstrings

Discussion in 'Java' started by -, Jul 15, 2005.

  1. -

    - Guest

    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) {
    ...
    }
     
    -, Jul 15, 2005
    #1
    1. Advertising

  2. -

    Hemal Pandya Guest

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

    - wrote:
    > 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 Pandya, Jul 15, 2005
    #2
    1. Advertising

  3. -

    - Guest

    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'.
     
    -, Jul 15, 2005
    #3
  4. -

    hiwa Guest

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

    - <> wrote in message news:<42d71ff2$>...
    > 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.
     
    hiwa, Jul 15, 2005
    #4
  5. -

    Hemal Pandya Guest

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

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


    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
    > > total length using regexp?

    >
    > 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.
     
    Hemal Pandya, Jul 15, 2005
    #5
  6. -

    - Guest

    hiwa wrote:
    > - <> wrote in message news:<42d71ff2$>...
    >
    >>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.
     
    -, Jul 15, 2005
    #6
  7. -

    - Guest

    Hemal Pandya wrote:
    > /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?
     
    -, Jul 15, 2005
    #7
  8. -

    Hemal Pandya Guest

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

    - wrote:

    >
    > 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.
     
    Hemal Pandya, Jul 15, 2005
    #8
  9. Re: Using regexp to check for length of string that acceptsoptional strings

    - <> writes:

    >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.
     
    Matthias Buelow, Aug 1, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    3
    Views:
    14,109
    Karl Seguin
    Feb 17, 2005
  2. Replies:
    5
    Views:
    667
    John W. Kennedy
    Jan 11, 2007
  3. Replies:
    5
    Views:
    942
    X-Centric
    Jun 30, 2005
  4. VijayRama

    Oledb length issue - Accepts only 255

    VijayRama, Nov 11, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    Alexey Smirnov
    Nov 11, 2009
  5. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
Loading...

Share This Page