Re: check string for different criteria



On 27-5-2005 17:08, Frank Meyer wrote:

Hi newsgroup,

not only that I have some trouble with my ArrayLists, I also need to check
Strings.

The problem is that I will have to check around 100 string if they meet the
specific criteria as follows:

XXX-000000

which means they have to start with three capital letters followed by a
hyphen and a six-digit number.

If anyone knows anything, I would be really thankful.

Once again best regards

Frank


Use a regular expression: [A-Z]{3}-\d{6}

In a Java string literal you have to escape the backslash, so this would become

if (  Pattern.matches("[A-Z]{3}-\\d{6}", theStringToCheck) ) {
   // accepted
} else {
   // rejected
}

<http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html>
--
Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \
.



Relevant Pages

  • Re: mot de passe root
    ... changing string lengths can be a source of trouble; ... And that's just with software that facilitates localization, ... in Windows programs than in most other types of software. ...
    (freebsd-questions)
  • Re: Fastest Date Sort
    ... So the whole program is done with String ... >>> But what kind of trouble could I be asking for? ... > default type conversions and formatting, ... and are best avoided by using explicit conversions. ...
    (comp.lang.basic.visual.misc)
  • Re: Win32: File Manipulation
    ... problems but I am having trouble manipulating the files once there. ... about an unquoted string "dnl". ... you're trying to "Put" the string $nextname concatenated with the ... Transfer of $nextname.txt to PTL server sucessful.\n"; ...
    (comp.lang.perl.misc)
  • Re: Fastest Date Sort
    ... > wise on the 'Date' type issue. ... So the whole program is done with String ... using the date datatype have always been wrought with trouble. ...
    (comp.lang.basic.visual.misc)
  • Help with operator overloading of <<
    ... I'm trying to play with classes and I'm having trouble with a ... who creates and writes to log files. ... string and then do the actual call to write the log line once. ...
    (comp.lang.cpp)