-1

I'm working on Python 2.7 and regular expressions. So my question is: how can I select the words with all the letters are uppercase from a .txt file? For example my txt file:

PYTHON 111 Python I - blabla. JAVA 222 Physics II - blalba..

I want an output like "PYTHON, JAVA". Also I don't want one letter words in my output.

Aaron Christiansen
  • 11,584
  • 5
  • 52
  • 78
  • 2
    What have you tried? Have you read about regex? Is the problem the file I/O or writing the regex? Do you know about character classes? If not, why not? – timgeb Apr 10 '16 at 15:57
  • 1
    Possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – ivan_pozdeev Apr 11 '16 at 02:19
  • Do you care about supporting Unicode? If so, what locales do you plan to support? – Tyler Crompton Apr 11 '16 at 03:19

2 Answers2

1

You don't need regular expressions. Of course if you like them, they can work, but python provides a simpler way:

>>> words = "PYTHON 111 Python I - blabla. JAVA 222 APPhysics II - blalba.."
>>> for word in words.split():
...   if word.isalpha() and word.isupper() and len(word) > 1:
...       print word
... 
PYTHON
JAVA
II
joel goldstick
  • 4,393
  • 6
  • 30
  • 46
0
>>> s="PYTHON 111 Python I - blabla. JAVA 222 APPhysics II - blalba.."
>>> re.findall(r"\b[A-Z]{2,}\b", s)
['PYTHON', 'JAVA', 'II']
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • you just provided a fish, it is more useful if you make an attempt to teach them how to fish. [fish saying](https://en.wiktionary.org/wiki/give_a_man_a_fish_and_you_feed_him_for_a_day;_teach_a_man_to_fish_and_you_feed_him_for_a_lifetime) I.E. explain how your solution actually works and what the regex pattern means, instead of just the code to accomplish the task. – Tadhg McDonald-Jensen Apr 10 '16 at 16:02
  • thanks for the answer can you explain what is the use of (r "...") – Pythonoob34 Apr 10 '16 at 16:11
  • r" means raw string. So you don't have to escape the \ – joel goldstick Apr 10 '16 at 16:14
  • @Pythonoob34 that's called a raw string. It's not strictly a regex concept but it's useful for that. Otherwise I would have had to write `re.findall("\\b[A-Z]{2,}\\b", s)` which is uglier, the difference being the double slash. – Alex Hall Apr 10 '16 at 16:14