I want to match all punctuation in my regexp except apostrophes. How do i do that in Ruby?

后端 未结 1 1836
孤独总比滥情好
孤独总比滥情好 2021-01-26 05:49

This is my code so far:

def alternate_words(string)
    string.gsub(/[\\p{P}]/, \"\")
end

I am looking for a way to add exceptions to my regula

相关标签:
1条回答
  • 2021-01-26 06:13
    string = "jack. o'reilly? mike??!?"
    puts string.gsub(/[\p{P}&&[^']]/, '')
    # => jack o'reilly mike
    

    Docs:

    A character class may contain another character class. By itself this isn’t useful because [a-z[0-9]] describes the same set as [a-z0-9]. However, character classes also support the && operator which performs set intersection on its arguments.

    So, [\p{P}&&[^']] is "any character that is punctuation and also not an apostrophe".

    0 讨论(0)
提交回复
热议问题