1

I am trying to get all the string that does not match these two regular expressions:

\d{2}[-]\d{2}[-]\d{4}

\d{2}[:]\d{2}

I have tried using:

/^((?!REGULAR_EXPRESSION_HERE).)*$/

But does not work.

Anyone know how it should be done? I can't use the re.split() function of python because I am using the regular expressions tool of Scrapy.

8
  • Sorry, what does brackets between - and : do? Commented Feb 11, 2014 at 13:23
  • @utdemir Oh, you can forget about them, it's the same if you remove them. I think that they are use when you want to work with a range [1-9]. Commented Feb 11, 2014 at 13:30
  • Python's regular expressions are not enclosed in slashes. Are you sure you're talking about Python? Commented Feb 11, 2014 at 13:32
  • @lanzz The two regulars expressions that I have put above are working properly in python. The other stuff, /^((?!REGULAR_EXPRESSION_HERE).)*$/, it's just something that I have found to calculate the opposite and it isn't working. Commented Feb 11, 2014 at 13:38
  • Can you use a replace or re.sub method? Commented Feb 11, 2014 at 13:42

1 Answer 1

1

Try this regex:

(?P<date>\d{2}-\d{2}-\d{4})|(?P<time>\d{2}:\d{2})

Sample code

#!/usr/local/bin/python

import re

text=("01-01-2014 13:15\n"
"foo bar\n"
"\n"
"Some text: blah bla !\n"
"Some ranges:\n"
"1-\n"
"-1\n"
"12-\n"
"-12\n"
"1-2\n"
"12-3\n"
"Strange date: 08-99-1999\n")

pattern = re.compile(r"(?P<date>\d{2}-\d{2}-\d{4})|(?P<time>\d{2}:\d{2})")

result = pattern.sub("", text)

print( result )

Output

\n 
foo bar\n
\n
Some text: blah bla !\n
Some ranges:\n
1-\n
-1\n
12-\n
-12\n
1-2\n
12-3\n
Strange date: \n

NOTA: I have added the \n otherwise they are not visible in the answer.

Tested on Python 2.7.4 and Python 3.2.3

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, I can't give you the vote up because I don't have the enough reputation yet... :(
@AritzBi I think you can now , dont'you ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.