1

I have text file like this:

...yolo wut is up dud! es.... heyo
eggssss hello...33421?

I tried doing this:

re.sub("[^a-zA-Z] ", "", string)

but it doesn't work, it leaves in numbers I want it to create the two lined text file into a string with spaces in between like this:

yolo wut is up dud es heyo eggssss hello
Nick
  • 118,076
  • 20
  • 42
  • 73

3 Answers3

0

The space in the quotes could be making it not match some parts.

Try using re.sub("[^a-zA-Z]", "", string)

  • doesn't change anything –  Jan 20 '20 at 00:42
  • Did you try putting the space inside the brackets? (“[^a-zA-Z ]”). 「Intended line feed stripped by site」 re.sub("[^a-zA-Z ]", "", string) (might mess up with the newline) 「Intended line feed stripped by site」 or 「Intended line feed stripped by site」 re.sub("[^a-zA-Z]", " ", string) (might add many extra spaces) – JohnSmith13345 Jan 20 '20 at 00:54
  • @JohnSmith13345 I think you might have forgotten the extra whitespace in your code. – AMC Jan 20 '20 at 02:38
0

To end up with a one line string, you can do this as a 2-stage process:

  1. remove all non-letter and whitespace characters
  2. replace sequences of whitespace characters with a single space

For example:

string = '''...yolo wut is up dud! es.... heyo
eggssss hello...33421?'''
print(re.sub(r'\s+', ' ', re.sub(r'[^a-zA-Z\s]', '', string)))

Output:

yolo wut is up dud es heyo eggssss hello
Nick
  • 118,076
  • 20
  • 42
  • 73
  • dude you're amazing, if you have time could you tell me what '\s+" does? and where it's from? where do i find all those commands –  Jan 20 '20 at 01:18
  • @FreelanceVideoandPhotoEdit no worries - I'm glad I could help. `\s+` matches one or more whitespace characters (space, tab, newline etc.). You might find [this Q&A](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) helpful. – Nick Jan 20 '20 at 01:20
  • @FreelanceVideoandPhotoEdit why did you change your question? – Nick Jan 20 '20 at 01:21
  • i got hacked i think –  Jan 20 '20 at 01:24
  • but it doesn't have the "+" what does the + do –  Jan 20 '20 at 01:25
  • @FreelanceVideoandPhotoEdit the `+` means "one or more" – Nick Jan 20 '20 at 01:26
  • mmm ok i will check it out for myself sorry to waste ur time –  Jan 20 '20 at 01:27
  • @FreelanceVideoandPhotoEdit No problem. – Nick Jan 20 '20 at 01:27
0

as \w matches every letter, using it in uppercase "\W" may have the effect that you want

>>> re.sub("\\W", "", "...yolo wut is up dud! es.... heyo")

'yolowutisupdudesheyo'

Edit

To keep the spaces, your code is almost right, you just have to fix the space position inside the first argument on re.sub... like this

>>> re.sub("[^a-zA-Z ]", "", string)

'yolo wut is up dud es heyo'

Vitalate
  • 106
  • 1
  • 3