From: Ype K. <yk...@xs...> - 2002-02-11 19:53:52
|
Doug, >I'm using the re module with Jython embedded in my Java app to handle the >pattern matching. I'm struggling with the regular expression syntax to >perform a negation of a pattern. Specifically, for a date I want to allow >DD-MMM-YY (JAN, MAR,etc for the month value) so I want my match object to >be returned only for items that don't match this pattern. > > r=re.compile('[0123][0-9][-]\w{3}[-][01][0123]').search("12-DEC-02") >//this returns a match object > >How can I wrapper this pattern and negate it so the failure to find the >pattern is what is returned? > >I've tried variations similar to the snippet below to group and negate >without success. >r=re.compile('^([0123][0-9][-]\w{3}[-][01][0123])').search("12-DEC-02") > >I have looked at the Mastering Regular Expressions book which is some help >but is heavily Perl slanted. The re module documentation doesn't delve >into my particular issue sufficiently (or I just missed it). Any >recommendations for additional reference resources in addition to some help >with the current problem would be welcome. Did you consider java's date formatter? I think it does exactly what you want: http://java.sun.com/j2se/1.4/docs/api/java/text/DateFormat.html You'll have to catch the ParseException from the parse() method to handle the failure to find the pattern. Good luck, Ype -- |