• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expressions in Java  RSS feed

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • 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
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can test out the pattern yourself by using the test harness provided with Sun's regular expression tutorial
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • 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.)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!