Delete words less than 4 in length
source link: https://www.codesd.com/item/delete-words-less-than-4-in-length.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Delete words less than 4 in length
This question already has an answer here:
- Remove small words using Python 2 answers
I am trying to remove words of length less than 4 from a string.
I use this regex:
re.sub(' \w{1,3} ', ' ', c)
Though this removes some strings but it fails when 2-3 words of length less than 4 appear together. Like:
I am in a bank.
It gives me:
I in bank.
How to resolve this?
Don't include the spaces; use \b
word boundary anchors instead:
re.sub(r'\b\w{1,3}\b', '', c)
This removes words of up to 3 characters entirely:
>>> import re
>>> re.sub(r'\b\w{1,3}\b', '', 'The quick brown fox jumps over the lazy dog')
' quick brown jumps over lazy '
>>> re.sub(r'\b\w{1,3}\b', '', 'I am in a bank.')
' bank.'
Related Articles
I counted the number of characters in a file but I want to count the number of words less than 5 and 6 or more
How can I grep for all words less than 4 characters?
Delete the text between parentheses if the text between the quotes has less than 3 words
PHP Regex: Delete words of less than 3 characters
C # WinForms Application - Debug Error - The length can not be less than zero. Parameter Name: Length
c # Regex removes words of less than 3 letters?
How to delete lines shorter than 3 words?
A lining to limit the word array to less than N number of characters in Ruby?
Delete a column in the DataFrame of a pandas if its sum is less than x
Oracle 10g: Can CLOB data lengths be less than 4,000?
SQL: Delete duplicate records if Date The difference between duplicate records is less than 30 days
Python - Deletes integers in an understanding list with less than two digits
Batch file to delete files containing less than 3 lines
The slice index greater than the length and less than the capacitance gives an error
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK