I am looking for a method that will only remove the first repeat occurence that is attached to the first.
For instance this:
removedup("user#gmail.com#gmail.com", "#gmail.com")
Should produce this
#gmail.com
"user#gmail.com#gmail.com".gsub(/(#gmail\.com)+/, '\1')
Related
Say I want to get rid of the first occurrence of 'my' in the following string:
my Marsha Tammy
My current regex setup is greedy I think:
.sub(/my/,"")
Which gets rid of all instances. Data will look like this:
my Bill Port
my Samy Gonzalez
my Ulm Germany
Only want first occurrence of 'my' gone.
Try .sub(/^my/,"")
This is a working example:
https://regex101.com/r/tD2jI2/1
EDIT: Or even better - .sub(/^my /,"") to get rid of the trailing space
According to the Ruby docs for String#sub:
Returns a copy of str with the first occurrence of pattern replaced by the second argument.
So, you should be in the clear in terms of it only replacing the first instance of your regexp. If you wanted it to replace all instances then you need to use String#gsub.
Suppose I have the following test string:
I want to match:
123
456
I want to use a regex to capture 123 and 456.
I'm trying this:
I want to match:(?:\n\s\s(\d*))*
but it only captures the last group. Any ideas?
I want to match:\K|\G(?!^)(?:\n\s\s(\d+))
You can use \G for this as regex engine only remembers the last group.See demo.
https://regex101.com/r/pG1kU1/13
You can use String#scan
s = <<-STR
123
456
STR
s.scan /\d+/ #=> ["123", "456"]
s.scan /(\d+)/ #=> [["123"], ["456"]]
Once you match the part I want to match: in the string, your next matching point will be somewhere after that. This means that you can only match once with that regex, given the format of your test string.
Unfortunately, you cannot get all the captured values of a capture group.
A good explanation is here, where it says:
The Returned Value for a Given Group is the Last One Captured
Since a capture group with a quantifier holds on to its number, what value does the engine return when you inspect the group? All engines return the last value captured. For instance, if you match the string A_B_C_D_ with ([A-Z])+, when you inspect the match, Group 1 will be D. With the exception of the .NET engine, all intermediate values are lost. In essence, Group 1 gets overwritten each time its pattern is matched.
So, if you know that the lines you want to capture will at most be 2, then you may try something like this:
I want to match:(?:\n\s\s(\d*))?(?:\n\s\s(\d*))?
i.e. manually repeat the capture group for each line.
You can capture your example string like this: \n*\s*(\d+)
how to ignore last character inside string in ruby? if i have problem like this, example :
abc = "123456a"
how to get result like this :
abc = "123456"
i don't need last character inside the string, how to ignore it?
thanks before :)
You can try:
abc.chop!
chop will delete the last character and ! will change the content in place.
Try this solution:
my_string[0..-2]
Negative indices index from the end, -1 is the last character; so to get everything up to next to last character, you'd write
abc[0..-2]
There are many ways to make it.
abc[0..-2]
abc[0..(abc.length-2)]
abc.delete abc[-1]
I'm currently studying regex (using ruby) and I would like to find the first occurrence of a non-digit character inside a word and make it capitalized.
I've been trying with:
word.gsub!(/\D{0,1}/) do |w|
w.capitalize
end
hoping it would just catch the first occurrence of a non-digit, but instead it returns all of the letters capitalized.
What's the correct way to do this?
Many thanks!!!
The g in gsub! stands for "global," meaning "every occurrence in the string." You want regular sub! instead. That'll just find the first. Cheers!
I am trying to replace a specific pattern in a text string.
That pattern is a href containing the word "sak".
My script currently looks like this:
ccontent=ccontent.sub(/<a .+?href=\"([^\"]+)\"[^\>]*>Sak<\/a>/, '')
The problem is that this replaces the entire string. (the string contains two links).
The problem is somewhere around the `a .+?" symbols, it runs through the link i want to Replace entirely and goes into the next link and replaces that whole link as well.
But I want it to STOP when the first pattern match is reached so that it only erases "sak" link.
How do i make the pattern match stop at the first time it reaches the 'href'?
Your expression is greedy, because .+? will actually keep matching any character as long as the pattern still matches.
Just use the [^>]* character set you're already using at the end of the regex:
ccontent.sub(/<a [^>]*href=\"([^\"]+)\"[^>]*>Sak<\/a>/, '')