Ruby Regex for repeated numbers in a string - ruby-on-rails

If i have a string like "123123123" - Here 123 is repeated 3 times.
1. So how can i get only "123" in ruby?
2. So if the string is "12312312" - Here 123 is repeated 2 times and then just 12, so here still i need to get "123".
3. Even if string is 99123123123, still i need to get 123.
Is this possible in Ruby Regex?
EDIT: I want this to solve Project Euler Problem 26 . So here 123 can be anything. All i want is to extract 1 number of at-least 2 repeated numbers.

This regex will detect all repeating groups.
(\d+)(?=.*\1)
Demo
Works great with ruby too.
result = '9912341234123'.scan(/(\d+)(?=.*\1)/)
#gets group with largest length
longestRepeatingGroup = result.max_by{|arr| arr[0].length}
puts longestRepeatingGroup
puts longestRepeatingGroup[0].length

Try this
99123123123.scan(/123/).count
12312312.scan(/123/).count

Related

Remove from a string the characters where bytesize is greater than 2 with Ruby

I have a problem with mysql and certain characters. If a user enters "hello ●", I obtain this error:
Mysql2::Error: Incorrect string value: '\\xE2\\x97\\x8F he...' for column 'subject'
I would like to exclude all characters whose bytesize is greater than two, i.e., keep French characters like é, à, ç, and remove emojis or characters like ●.
Given string = "hèllö>●!", I would like to obtain "hèllö>!". In order to do so, I wrote this:
def bytesize(var)
var.each_char do |char|
puts char.bytesize
end
end
bytesize(string)
1
2
1
1
2
1
3
1
# => "hèllö>●!"
which is not what I expected. What is the best way to remove from all characters whose the bytesize is greater than two from a string?
I don't do that in the model because I can manage this with a gem, but my problem appears when a job wants to put the string in the logs of Amazon SES.
Elaborating on OP's efforts, not using regular expressions:
string = "hèllö>●!"
cleaned = string.each_char.with_object("") do |char, str|
str << char unless char.bytesize > 2
end
p cleaned
I suspect that you are getting that error message because you have the wrong column text encoding. If you are using Unicode in your system, and this day and age you should be, your column type should be utf8mb4. See this on how to change your column types.
Taking your comment into account the following will remove any characters outside the BMP
sentence.gsub(/[\u{10000}-\u{10FFFF}]/,'')

Lua script - find digits in a string

I have a Cisco ASA 8.4 VPN Concentrator. I am trying to use Lua to extract digits from a certificate string coming in and use them in a LDAP lookup with AD for authorization. I found a string that works...sometimes.
The string comes in with the format:
LAST_NAME.FIRST_NAME.MIDDLE_NAME.1234567890
My LDAP only wants to see the digits and #domainname. The script I am currently us is: return string.gsub(cert.subject.cn, "^(%w+)%.(%w+)%.(%w+)%.(%w+)$", "%4#domain")
This script works fine in most cases (80-90% of the time). When it doesn't work is when people have no middle name, 4 names instead of 3, etc.
My question is how can I get it to output only the 10 digits, regardless of what comes before it. Seems too easy with a return string.match, but so far I can't get it to work. Any ideas?
You can use the pattern .*(%d%d%d%d%d%d%d%d%d%d)$:
local str = 'LAST_NAME.FIRST_NAME.MIDDLE_NAME.1234567890'
print(str:match('.*(' .. ('%d'):rep(10) .. ')$'))
or .*(%d+)$ if the number of digits is always 10.
If the 10 digits is always the last 10 characters, this works:
print(str:sub(-10, -1))

Isolating/removing Characters from string using rails

I am using ruby on rails
I have
article.id = 509969989168Q000475601
I would like the output to be
article.id = 68Q000475601
basically want to get rid of all before it gets to 68Q
the numbers in front of the 68Q can be various length
is there a way to remove up to "68Q"
it will always be 68Q and Q is always the only Letter
is there a way to say remove all characters from 2 digits before "Q"
I'd use:
article.id[/68Q.*/]
Which will return everything from 68Q to the end of the string.
article.id.match(/68Q.+\z/)[0]
You can do this easily with the split method:
'68Q' + article.id.split('68Q')[1]
This splits the string into an array based on the delimiter you give it, then takes the second element of that array. For what it's worth though, #theTinMan's solution is far more elegant.

Forecast.io Ruby Gem - Remove Decimal from Temperature

I am using the forecast-ruby gem and everything is working fine. However, when you get the temperature from the API, it returns it with decimals, which I don't need. (e.g., 85.23). I would like to strip the last 2 digits and just display the temperature as 85 degrees.
Here is what I am working with:
#forecast = ForecastIO.forecast(lat, long)
Then, I can check the current temp with: #forecast.currently.temperature => 85.23
Does anyone know of a way to either request just the basic temperature from the API? If not, how would I go about removing the last three characters within the method above to achieve an end result of just 85?
Thanks!
It appears that you're not dealing with characters, it's a number, so just use .round:
x = 85.23
x.round # 85
If you want to actually truncate to 85 (even if the temperature were say 85.99) then use .to_i instead.

matching an address with regex doesn't match the target part

I'm not quite good in regex.
With my input string LT 1 BLK 4 LAKES OF PARKWAY 5 R/P & AMEND
I'd like to match just the only part between the figure 4 and 5 in the string.
meaning that, my expected result is LAKES OF PARKWAY.
I've tried to come up with a pattern to get such result.
\d+\s+([A-z ]+)(\d+.*?)*$
but with my pattern, it only matches BLK and 5 R/P & AMEND, as group #1 and group #2 respectively. At the end of my thought pattern, I decide to use end of string matching, $.
So, when 5 R/P & AMEND got matched, the pointer should move further behind to the sub sequence part. Then, ([A-z ]+) should match LAKES OF PARKWAY.
What's wrong with my pattern? and how to get it to work?
Any advice would be very much appreciated.
Try \d+\s+(\D+)\d+\D*$
\D means 'anything that is not \d, so it won't be allowed to match, for example, between the first 1 and 4, because then the ending of the regex would be rejected at the later 5.

Resources