2

I want to differentiate between two python strings 2015-07-01 and 2015-Jul-01

Case 1: Digits OR Special characters

Case 2: Everything else (characters)

I'm aware of the isdigit() function but it only recognized digits and not special characters.

By special characters I mean hyphen - or slash /

Community
  • 1
  • 1
Harvey
  • 184
  • 1
  • 3
  • 15
  • 1
    define special character and non-special character – timgeb Feb 16 '16 at 17:02
  • 2
    Suggested reading: [What is the XY problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Vincent Savard Feb 16 '16 at 17:03
  • hyphen `-` or slash `/` – Harvey Feb 16 '16 at 17:03
  • Following on what @VincentSavard is alluding to, if you are asking because you are about to try to parse time stamps yourself, stop and go look up the [`datetime`](https://docs.python.org/3/library/datetime.html) library – Cory Kramer Feb 16 '16 at 17:04
  • @CoryKramer I've already referred `datetime`. I know it might seem like a vague question but the problem is that when you want to parse date from string you have to define a specific format with help of directives. Now the data I receive can be of both case 1 & 2. So how am I suppose use the directives to serve both the cases? – Harvey Feb 16 '16 at 17:08
  • I also though of using enumeration to substitute the months with their numeric counterparts but thats not the most efficient and smartest way to deal the problem I guess. – Harvey Feb 16 '16 at 17:10

5 Answers5

10

You can use a regular expression to check whether a string contains only digits, hyphens or slashes.

>>> bool(re.match('[\d/-]+$', '2015-07-01'))
True
>>> bool(re.match('[\d/-]+$', '2015-Jul-01'))
False

We don't need the ^ anchor here because match starts from the beginning of the string.

Alternatively, with all and without a regex:

>>> from string import digits
>>> allowed = set(digits).union('/-')
>>> all(c in allowed for c in '2015-07-01')
True
>>> all(c in allowed for c in '2015-Jul-01')
False
timgeb
  • 76,762
  • 20
  • 123
  • 145
4

Assuming data is a string and you want to check that there are no "letter" characters in it..

if not any(c.isalpha() for c in data):
   print('The string contains no letters')
Chad S.
  • 6,252
  • 15
  • 25
2

You can check if the string has any letters.

bool(re.search('[a-zA-Z]', the_string))
melalonso
  • 228
  • 2
  • 9
2

The other answers address your question title much better than this. But I thought it wouldn't harm to point out that, now we know your specific case is to deal with just two date formats, that I have found the dateparser module pretty good for this rather than regex or exceptions when there can be a mix.

import dateparser

date1 = '2015-07-01'
date2 = '2015-Jul-01'

date1out = dateparser.parse(date1)
date2out = dateparser.parse(date2)

print date1out
print date2out
print '\n'
print date1out.date()
print date2out.date()

Will give you

2015-07-01 00:00:00
2015-07-01 00:00:00

2015-07-01
2015-07-01
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • Sure it would work but if I'm not wrong the `dateparser` works on _hit & miss_ kind of logic which has a higher complexity which we can't afford just for two types of data. Its more useful when we are dealing with various kinds of formats. – Harvey Feb 16 '16 at 17:28
  • @Harvey For the particular case you listed it has worked fine for me as the listed behaviour is predictable. But yes, if you were dealing with multiple formats then I wouldn't trust it e.g. in cases with American and English formats with dd/mm/yyyy and mm/dd/yyyy. Don't think I've ever seen yyyy/dd/mm so the default behaviour here shouldn't ever be off? Then again, if you had more than just these two formats then the regex answers here still wouldn't allow you to specify the `datetime` format anyway without an exception? – roganjosh Feb 16 '16 at 17:32
  • Indeed, the `dateparser` is more versatile but it comes at a higher complexity which isn't visible until you deal with large data. – Harvey Feb 16 '16 at 17:45
0
import string
word = '2015-07-01'
invalidChars = set(string.punctuation)
if any(char in invalidChars for char in word):
    print ("Contains special characters")
else:
    print ("No special characters")

The above code will tell yu if the string contains special characters, but unless you are more specific about what you want the program to produce, I cannot help you further.

D.C
  • 120
  • 9