Looking for some Regex help in Ruby.
I have a string that is formated as so:
YYYY-MM-DD-title-of-post.markdown
I'm looking to use gsub or sub to remove the YYYY-MM-DD portion of the string and do further processing to it.
I would like a Regular Expression to remove the date from the string. The date will never be in a different format.
Thanks for the help!
This should do the trick:
new_title = old_title.gsub('\d{4}-\d{2}-\d{2}','')
If you are confident that the date will ALWAYS be in the format YYYY-MM-DD and will ALWAYS appear at the beginning of the string than the following regex will work:
my_string = "YYYY-MM-DD-title-of-post.markdown"
date = my_string.match(/^(\d{4}-\d{2}-\d{2})-.*/)[1]
Related
Using a regex, how can I match strings that end with exactly one . as:
This is a string.
but not those that end with more than one . as:
This is a string...
I have a regex that detects a single .:
/[\.]{1}\z/
but I do not want it to match strings that end in ....
What you want is a 'negative lookbehind' assertion:
(?<!\.)\.\z
This looks for a period at the end of a string that isn't preceded by a period. The other answers won't match the following string: "."
Also, you may need to look out for unicode ellipsis characters…
You can detect this like so: str =~ /\u{2026}/
You can use:
[^\.][\.]\z
You are looking for a string that before the last dot there is a char that is not a dot.
I like Regexr a lot!
Solution similar to Dekel:
[^.]+[.]
Live demo
I have a string that represents a date:
"12.27.1995"
I need to swap the month and the day to get:
"27.12.1995"
I did:
date = "12.27.1995"
month = date[0]+date[1]
day = date[3]+date[4]
date[0] = day[0]
date[1] = day[1]
date[3] = month[0]
date[4] = month[1]
It works good, but looks to bad for me. Is it possible to make it more reliable using less code?
Since your string represents a date, you might want to use a Date object, with strptime to parse the original string and strftime to output it in the desired format:
require 'date'
date = Date.strptime("12.27.1995", "%m.%d.%Y")
puts date.strftime("%d.%m.%Y")
# 27.12.1995
Yes. Perhaps like this:
date = "12.27.1995"
m, d, y = date.split(".")
date = [d, m, y].join(".")
While the answer by #sawa is perfectly valid and should be used here, I would show some technic which is wrong and should not be used here, but might be helpful for anybody to swap two fixed parts of the string:
"12.27.1995".tap { |s| s[0..1], s[3..4] = s[3..4], s[0..1] }
#⇒ "27.12.1995"
Not elegant, but a way using regex captures:
/(\d{1,2})\.(\d{1,2})\.(\d{4})/.match "12.27.1995"
[$2, $1, $3].join('.') #=> "27.12.1995"
I have a file details.txt in which data stored is in this format
"571955NandhithaF1975-12-222011-12-06Mumbai"
Columns are first six digit unique id ,
name , (M/F) Gender , dob,joining date , and location
i have to separate this in six columns using comma delimiter !!
Please help me in this problem
Pass each line into a regex function which contains the below logic :
String expression = "571955NandhithaF1975-12-222011-12-06Mumbai";
Pattern pattern = Pattern
.compile("([0-9]{6})([a-zA-Z]+)([M|F])([0-9]{4}-[0-9]{2}-[0-9]{2})([0-9]{4}-[0-9]{2}-[0-9]{2})([a-zA-Z0-9]+)");
Matcher matcher = pattern.matcher(expression);
if (matcher.find()) {
//System.out.println(matcher.group());
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
System.out.println(matcher.group(5));
System.out.println(matcher.group(6));
}
output:
571955
Nandhitha
F
1975-12-22
2011-12-06
Mumbai
571955NandhithaF1975-12-222011-12-06Mumbai
To split this type of data, we have to use String Functions in java in the mapper class under map method.
You can use substring(beginindex,endindex) method to get Id of from the string, its
like string id[6]=substring(0,5) which returns 6 digit string that is ID.(As ID is Fixed length we take 6)
You can use substring(beginindex) to get remaining string.
Next on wards you have to use REGXP in java.. along with split(regexp) to get the name, gender, dob, doj, loc. But definitely some workout with java takes place.
go through this link for String functions in java.
Hope this post may help.
If any suggestions or modifications to the same are also accepted :)
I have a pseudo-code method that works like this:
def my_method(file)
while(line = file.gets)
case line
when /^TEXT (.*)/
puts line + <the text captured in the parenthesis of the regex>
else
.....
end
end
end
Is there any way to do this?
EDIT:
The sample string is like:
TEXT a sample text
I want to have "a sample text" captured by the regex. I know this is not the proper way to do this, but this is just a demonstration, i.e. "YYYY-MM-DD format date in shell script" to figure out how to get the date in whatever format you want.
Yesterday's date can be found as:
date -d '1 day ago' +'%Y/%m/%d'
from "How To Get Yesterday’s Date using BASH Shell Scripting".
Replace the / with - or _ and then pass them in to the Ruby statement.
Edit: Vote for the other guy. Their answer actually has code.
What you are looking for is puts "line#{$1}". The pseudo-globals $1, $2, $3, etc. refer to capture groups of the last Regexp match. (And $~ refers to the MatchData itself, if you'd like to work with that.)
I have a string like this (YYYYMMDD):
20120225
And I want to have a string like this (MM/DD/YYYY):
02/25/2012
What's a good way of doing this in Ruby? I have thought about converting the first string to a Date, and then change the format. Or just treating the string and getting the parts I want and build the new string.
Parsing it then formatting it is the best solution:
Date.parse("20120225").strftime("%m/%d/%Y") #=> "02/25/2012"
strptime parses the string representation of date with the specified template and creates a date object.
Date.strptime('20120225', '%Y%m%d').strftime("%m/%d/%Y") #=> "02/25/2012"
Just for fun how about:
'20120225'.unpack('A4A2A2').rotate.join('/')
It's possible with regular expressions:
s1 = '20120225'
s2 = "$2/$3/$1" if s1 =~ /(\d{4})(\d{2})(\d{2})/
Or if you're sure of the format of your string and have performance issues, I think the best solution is
s2 = s1[4..5] + '/' + s1[6..7] + '/' + s1[0..3]
But if you have no performance needs, I think the solution of Andrew Marshall is better because it checks the date validity.