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."
5
  • 1
    Could you please clarify with an example what you are doing? Also, see Testing whether string starts with or end with another string. Commented May 12, 2017 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 from your previous question. Commented May 12, 2017 at 21:24
  • 1
    @AndrewWhitaker, I'd like to remove the entire string. Commented May 12, 2017 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. Commented May 12, 2017 at 21:37
  • The best answer in the first linked page is 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. Commented May 12, 2017 at 21:56

3 Answers 3

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?.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-2
def replace_suffix(str,suffix)
  str.end_with?(suffix)? str[0, str.length - suffix.length] : str 
end

5 Comments

Please add some explanation
it would help if you actually wrote what's your question, the context, what's the problem, and what you want from it...
The goal is to replace substring in string if and only if substring is string's suffix. all the rest should be left alone.
While this code snippet may solve the question, including an explanation 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!
what explanatory comments?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.