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]
Related
I have seen many methods for removing some characters based on regular string or special match using tr etc. But I didn't find any way to delete a character based on accurate index in a string.
For example: In var="hello world",I want to delete the 5th char 'o' and then var should be like "hell world". The code should apply to all other normal strings. Thanks in advance.
One method is:
n=5
var="hello world"
var=${var:0:n-1}${var:n}
I would like to achieve this amazing result (I'm using Ruby):
input: "Joe can't tell between 'large' and large."
output: "Joe can't tell between large and large."
getting rid of the quotes but not of the apostrophe
how can I do it in a simple way?
my failed overcomplicated attempt:
entry = test[0].gsub(/[[']*1]/, "")
Simplest one for your situation could be something like this.
Regex: /\s'|'\s/ and replace with a space.
Regex101 Demo
You can also go with /(['"])([A-Za-z]+)\1/ and replace with \2 i.e second captured group.
Regex101 Demo
Here's a script to demo an answer:
x = "Joe can't tell between 'large' and large."
puts x.gsub(/'\s|\s'/, " ")
# Output: Joe can't tell between large and large.
To decode what this script does - the gsub / regex line is saying:
Find all (an apostrophe followed by a space '/s) or (a space
followed by an apostrophe \s') and replace it with space.
This leaves apostrophes that aren't adjacent to spaces intact, which seems to remove only the apostrophes the OP is trying to remove.
Maybe this one?
entry = test[0].gsub(/[^']/, "")
But it should remove all '.
This does exactly what you are looking for, including ignoring the posted comments Students' example.
entry = test[0].gsub(/'([^\s]+)'/, '\1')
I don't have ruby set up, but i confirmed this works here: http://tryruby.org/levels/1/challenges/0
Here is an example on regex101:
https://regex101.com/r/aY8aJ3/1
I need a regex that returns the first N words from a string, including line breaks and white spaces. I tried with the following code, but the server crashes:
str[/\S+(\s)?{N}/].strip
Like this (for the first 15 words):
if subject =~ /^(?:\w+\s){15}/
thefirstwords = $&
Just change the 15 to whatever number you like.
I guess you can achieve this without even regex:
str.split[0...n].join(' ')
Try this expression
'/^.\S+(\s){N}/'
Start with any character and match up to N words.
I'm trying to get the first word in this string: Basic (11/17/2011 - 12/17/2011)
So ultimately wanting to get Basic out of that.
Other example string: Premium (11/22/2011 - 12/22/2011)
The format is always "Single-word followed by parenthesized date range" and I just want the single word.
Use this:
str = "Premium (11/22/2011 - 12/22/2011)"
str.split.first # => "Premium"
The split uses ' ' as default parameter if you don't specify any.
After that, get the first element with first
You don't need regexp for that, you can just use
str.split(' ')[0]
I know you found the answer you are needing but in case anyone stumbles on this in the future, in order to pull the needed value out of a large String of unknown length:
word_you_need = s.slice(/(\b[a-zA-Z]*\b \(\d+\/\d+\/\d+ - \d+\/\d+\/\d+\))/).split[0]
This regular expression will match the first word with out the trailing space
"^\w+ ??"
If you really want a regex you can get the first group after using this regex:
(\w*) .*
"Single-word followed by parenthesized date range"
'word' and 'parenthesized date range' should be better defined
as, by your requirement statement, they should be anchors and/or delimeters.
These raw regex's are just a general guess.
\w+(?=\s*\([^)]*\))
or
\w+(?=\s*\(\s*\d+(?:/\d+)*\s*-\s*\d+(?:/\d+)*\s*\))
Actually, all you need is:
s.split[0]
...or...
s.split.first
Here's my string:
http://media.example.com.s3.amazonaws.com/videos/1/123ab564we65a16a5w_web.m4v
I want this: 123ab564we65a16a5w
The only variables that will change here are the /1/ and the unique key that I'm trying to pull. Everything else will be exactly the same.
For the /1/ portion, that 1 could be anywhere from 1-3 digits, but will always be numeric.
I'm running Ruby 1.9.2.
Assuming nothing else changes, here's the regex for it:
http://media.example.com.s3.amazonaws.com/videos/\d{1,3}/(.*)_web.m4v
If there are other changes, you need to let us know all the variables.
This is shorter -
s.split(/[/_.]/)[-3]
Since you've indicated that the value you want will always have a "/" immediately before it (and none after it) and an "_" immediately after it, you could use this generic regex:
^.*/(.*)_.*$
Here's why this would work:
^ matches the beginning of the line
.*/ matches any number of characters up to the slash - this is greedy, so it will go until the last slash in the input value
(.*) matches any number of characters and captures the result
_.* matches an underscore and then any number of characters
$ matches the end of the line
By matching anything up to the last "/" and then anything after the "_", you easily isolate the desired value.
NOTE: I don't know if the Ruby regex syntax is any different than this, so your mileage may vary.
--
EDIT: It looks like in Ruby, you might not need/want the ^ or $ at the beginning and end.