• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular Expressions in Java

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have to right a regular expression in Java, The field name is file description, in order for file description to be a valid field the criteria is

The field is required and the text should not contain any special characters, line feeds or carriage returns. The length of the text should not exceed 255 characters.

("[.+-[\\W\\n\\r]]{1,255}")

Please let me know if this pattern is correct.

Thank you very much
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can test out the pattern yourself by using the test harness provided with Sun's regular expression tutorial
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you're trying to do set subtraction, but that isn't how it's done. Instead, you have to use the intersection operator, &&, with a negated character class. For example, if you wanted to match any punctuation character except a period or plus sign, you would useBut for your puropose, I don't think you need anything so elaborate. If you want to match any word character or plus sign or period, just say that directly:(BTW, this intersection notation is unique to the java.util.regex package.)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic