Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract leading and trailing punctuation from a word for better matching #2

Open
talk2MeGooseman opened this issue Aug 14, 2020 · 0 comments

Comments

@talk2MeGooseman
Copy link
Collaborator

Both leading and trailing punctuation need to be checked and extracted from around a word so that the string matching and replace can happen. Afterward, the punctuation will be combined again to preserve the structure.

  def extract_punctuation(word)
      leading_punctuation  = word.match(/\A([^a-zA-Z]*)/)[1] rescue ''
      trailing_punctuation = word.match(/[a-zA-Z]+([^a-zA-Z]*)\Z/)[1] rescue ''
      word_length = word.length - leading_punctuation.length - trailing_punctuation.length
      word = word[leading_punctuation.length, word_length]
      return leading_punctuation, word, trailing_punctuation
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant