0

I am new to regex and looked into the possible matching expression. But I can't able to find the thing I needed. What I want is remove the word that matches certain expression. The expression is to match all the words ignoring whitespaces, tabs and newline. I am not able to find the correct regex in ruby.

For example:

string = 'hello world welcome'

The thing I need to do is to replace certain words matches with word starting with 'w' and end with 'd'.

string.gsub(/^w.*d$/, 'human')

but I am not able to ignore(escape) spaces(not replacing), tabs and new lines in that.

Can someone help with this. I tried with this below regexp to escape but its not happening.

string.gsub(/^w.*d$\s/, 'human')

'hello world welcome' must be changed to 'hello human welcome' without removing spaces, tabs and newlines in the string.

Is that anywhere I can know more about regexp especially with ruby.

1
  • 2
    It appears you may want something like string.gsub(/\b\w[a-z]*d\b/i, 'human') #=> "hello human welcome". You don't want either anchor. ^ would require 'world' to be at the beginning of the line to be matched and hence replaced. Similarly, $ would require 'world' to be at the end of the line to be matched. The word boundaries, \b (aka "word breaks"). prevent a match if the string were 'Hello underworld welcome' and 'Hello worldly welcome'. /i makes the regex case-indifferent, so it will match, say, 'World'. Commented Jun 14, 2020 at 6:36

2 Answers 2

1

If I properly understand your question the regular expression would be:

\bw[a-z]*d

See the pattern in action on Regexr, which helps generate regular expressions and tells you exactly what you are doing.

Cary Swoveland's response is also good.

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

4 Comments

Thanks:). But why it doesn't work for special characters while I tired \b{[a-z]*} it doesn't work. And also I tried escaping characters \b\{[a-z]*\}
@hello: Don't try to change your question in comments. Please, edit your question and add real test cases with exected result.
What do you mean by it doesn't work? It is just going to select a different range of words. \{ is an escaped character, meaning that it is going to look for characters that start with "{". But if you want your RegEx to actually capture words withing the curly braces then \{[a-z]*\}. You can also take a look at this \{\bw|W[a-zA-Z]*d|D\}.
You can use the resources left by Todd A. Jacobs to further your knowledge.
0

Problem

Your regex has many problems, including:

  1. being improperly anchored to the beginning and end of the line
  2. using greedy matchers
  3. using String#gsub instead of String#sub when you only want to replace only the first match

You can learn more about Ruby's regex engine and expressions in the documentation for the Regexp class. O'Reilly also publishes many books about regular expressions, and the different features found in several popular languages and regular expression engines/implementations.

Solution

There's more than one way to achieve your goal with this specific string. The following will work:

string = "hello world welcome"

# specify string instead of regex pattern
string.sub "world", "human"
#=> "hello human welcome"

# replacement using word boundaries
string.sub /\bworld\b/, "human"
#=> "hello human welcome"

# unanchored, non-greedy matching
string.sub /\bw.*?\b/, "human"
#=> "hello human welcome"

All examples yield the same results. The first example is likely best, as you already know what word you want to replace and can simply specify the string. The other examples leverage:

  • \b as a zero-width assertion to anchor the string or pattern using word boundaries
  • *? to define a non-greedy pattern that will match the shortest possible match

These solutions all work with your posted samples. Your mileage may vary with other inputs.

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.