0

Based on "How to Delete Strings that Start with Certain Characters in Ruby", I know that the way to remove a string that starts with the character "@" is:

email = email.gsub( /(?:\s|^)@.*/ , "") #removes strings that start with "@"

I want to also remove strings that end in ".". Inspired by "Difference between \A \z and ^ $ in Ruby regular expressions" I came up with:

email = email.gsub( /(?:\s|$).*\./ , "") 

Basically I used gsub to remove the dollar sign for the carrot and reversed the order of the part after the closing parentheses (making sure to escape the period). However, it is not doing the trick.

An example I'd like to match and remove is:

"a8&23q2aas."
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
HMLDude
  • 1,547
  • 7
  • 27
  • 47
  • 1
    Could you please clarify with an example what you are doing? Also, see [Testing whether string starts with or end with another string](http://stackoverflow.com/questions/11715637/testing-whether-string-starts-with-or-end-with-another-string). – Wiktor Stribiżew May 12 '17 at 21:23
  • Do you want to remove strings from an array that end with `.`? Or do you want to remove the trailing `.` from strings that end with that character? It seems like you could just do `ends_with?` with [this solution](http://stackoverflow.com/a/43691506/497356) from your previous question. – Andrew Whitaker May 12 '17 at 21:24
  • 1
    @AndrewWhitaker, I'd like to remove the entire string. – HMLDude May 12 '17 at 21:35
  • @WiktorStribiżew I would really like to stick to using gsub, if at all possible. But based on the code on the page you suggested, I could probably create a workaround. – HMLDude May 12 '17 at 21:37
  • The best answer in the first linked page is http://stackoverflow.com/a/43691506/128421. The others have liabilities that won't be apparent at first but would slow code that relies on them significantly. – the Tin Man May 12 '17 at 21:56

3 Answers3

2

You were so close.

email = email.gsub( /.*\.\s*$/ , "")

The difference lies in the fact that you didn't consider the relationship between string of reference and the regex tokens that describe the condition you wish to trigger. Here, you are trying to find a period (\.) which is followed only by whitespace (\s) or the end of the line ($). I would read the regex above as "Any characters of any length followed by a period, followed by any amount of whitespace, followed by the end of the line."

As commenters pointed out, though, there's a simpler way: String#end_with?.

Derrell Durrett
  • 544
  • 10
  • 26
1

I'd use:

words = %w[@a day in the life.]
# => ["@a", "day", "in", "the", "life."]

words.reject { |w| w.start_with?('@') || w.end_with?('.') }
# => ["day", "in", "the"]

Using a regex is overkill for this if you're only concerned with the starting or ending character, and, in fact, regular expressions will slow your code in comparison with using the built-in methods.

I would really like to stick to using gsub....

gsub is the wrong way to remove an element from an array. It could be used to turn the string into an empty string, but that won't remove that element from the array.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
-2
def replace_suffix(str,suffix)
  str.end_with?(suffix)? str[0, str.length - suffix.length] : str 
end
Ehud
  • 105
  • 5
  • Please add some explanation – MrLeeh May 03 '18 at 14:27
  • it would help if you actually wrote what's your question, the context, what's the problem, and what you want from it... – jjmerelo May 03 '18 at 16:26
  • The goal is to replace substring in string if and only if substring is string's suffix. all the rest should be left alone. – Ehud May 03 '18 at 18:35
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor May 04 '18 at 21:17
  • what explanatory comments? – Ehud May 06 '18 at 12:05