KobbyAdarkwa
KobbyAdarkwa

Reputation: 181

Ruby: Remove all exclamation marks from sentence except at the end

Working on a code war challenge to remove all exclamation marks from the string except for the end. So far I've played around with some regex and fortunately enough I haven't recieved any error messages so it at least recognises my code as a valid expression.

def remove(s)
s.gsub(/\s+/, "!")
end

Could I please get some input on how I can write this regex to remove "!" from everywhere else in the string but the end? If I could possibly also get a method without regex that will be superb as regex can be a tad confusing.

For reference this is what the tests should look like:

Test.describe("Basic tests") do
    Test.assert_equals(remove("Hi!"),"Hi!")
    Test.assert_equals(remove("Hi!!!"),"Hi!!!")
    Test.assert_equals(remove("!Hi"),"Hi")
    Test.assert_equals(remove("!Hi!"),"Hi!")
    Test.assert_equals(remove("Hi! Hi!"),"Hi Hi!")
    Test.assert_equals(remove("Hi"),"Hi")
end

Upvotes: 0

Views: 767

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627128

Remove all ! except all those at the end of string with

s.gsub(/!(?!!*\z)/, "")

See the Rubular demo and a regex101 demo (converted to PCRE to work with multiline string).

Details

  • ! - matches a ! sign
  • (?! - start of a negative lookahead that matches a location not immediately followed with:
    • !* - zero or more ! chars up to
    • \z - the end of string
  • ) - end of the lookahead.

You may extend the regex to replace any punctuation char unless at the end of string:

s.gsub(/([[:punct:]])(?![[:punct:]]*\z)/, "")

where [[:punct:]] matches any punctuation char (can be replaced with [\p{P}\p{S}]).

Upvotes: 3

steenslag
steenslag

Reputation: 80085

Non regex: take the string except the last char, remove the exclamation marks from that, add the last char back on.

def remove(str)
  return "" if str.empty?  # defend against nil-errors
  str[0..-2].delete("!") + str[-1]
end

Upvotes: 2

Related Questions