How do I remove certain words from a string, but only if they appear at the end of the string?

Ringo Blancke

I have company names like "The Millard Group" and "The Chimney Corporation". I want to remove the words like "Group" or "Corporation", but only if they appear at the very end of the word. I.e. I don't want to remove them if they appear somewhere in the middle.

How can I go about doing this in Ruby? gsub will replace the string from wherever it is, and also I have a list of about ten, so I'd rather not run gsub ten times. It would be great if I could provide an array of these words to remove.

Eugene

Try something like this:

['The Millard Group', 'The Chimney Corporation'].each do |s|
  s.gsub!(/\ (Group|Corporation)$/, '')
end

You can add more words to remove in the regular expression by adding them after more |

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove certain words from string

From Dev

How to remove only certain substrings from a string?

From Dev

how to remove characters only from the end of a string?

From Dev

Python: How to remove certain words in a string

From Dev

Remove "\n" string from end of certain lines

From Dev

How do I check a string for certain words without getting matches for parts of the words in the string in Python?

From Dev

How do I delete 4 characters from the end of certain lines in a string?

From Dev

How do I delete 4 characters from the end of certain lines in a string?

From Dev

Extracting only certain words from string and ignoring words with numbers, etc

From Dev

How can I remove the special character words from a string?

From Dev

How do I remove CSS from a string?

From Dev

How Do I Remove A Vowel From String

From Dev

How do i remove chars from a string?

From Dev

How do I remove punctuaution from a string

From Dev

how do i remove a letter from string

From Dev

How can I remove certain characters from a string?

From Dev

How can I remove certain characters from a string?

From Dev

How can I remove certain string from file name?

From Dev

How to get words that end with certain characters within each string r

From Dev

how do I get rid of selected words from a string

From Dev

How to get words starting with # and remove from string

From Dev

How to get words starting with # and remove from string

From Dev

How do I remove certain characters from multiple words in my file?

From Dev

How do I grep a string from lines that contain a certain number?

From Dev

How do I count the words in a string using only loops and string.strip()?

From Dev

Regex Expression to remove certain characters from string expect at the start and end

From Dev

How to remove certain characters from a string? [Python]

From Dev

How to take only certain value from string?

From Dev

How to remove substring from the end of the string

Related Related

  1. 1

    Remove certain words from string

  2. 2

    How to remove only certain substrings from a string?

  3. 3

    how to remove characters only from the end of a string?

  4. 4

    Python: How to remove certain words in a string

  5. 5

    Remove "\n" string from end of certain lines

  6. 6

    How do I check a string for certain words without getting matches for parts of the words in the string in Python?

  7. 7

    How do I delete 4 characters from the end of certain lines in a string?

  8. 8

    How do I delete 4 characters from the end of certain lines in a string?

  9. 9

    Extracting only certain words from string and ignoring words with numbers, etc

  10. 10

    How can I remove the special character words from a string?

  11. 11

    How do I remove CSS from a string?

  12. 12

    How Do I Remove A Vowel From String

  13. 13

    How do i remove chars from a string?

  14. 14

    How do I remove punctuaution from a string

  15. 15

    how do i remove a letter from string

  16. 16

    How can I remove certain characters from a string?

  17. 17

    How can I remove certain characters from a string?

  18. 18

    How can I remove certain string from file name?

  19. 19

    How to get words that end with certain characters within each string r

  20. 20

    how do I get rid of selected words from a string

  21. 21

    How to get words starting with # and remove from string

  22. 22

    How to get words starting with # and remove from string

  23. 23

    How do I remove certain characters from multiple words in my file?

  24. 24

    How do I grep a string from lines that contain a certain number?

  25. 25

    How do I count the words in a string using only loops and string.strip()?

  26. 26

    Regex Expression to remove certain characters from string expect at the start and end

  27. 27

    How to remove certain characters from a string? [Python]

  28. 28

    How to take only certain value from string?

  29. 29

    How to remove substring from the end of the string

HotTag

Archive