Find and Replace Regular Expression (NotePad++) - regex

I'm looking at a global search and replace within NotePad++ which will look for the following text...
date and %c
examples would be...
date +%m-%d%H:%M:%S%c
date +%m-%d%H%c:%M:%S
date +%c-%m-%d%H:%M:%S
etc.
I can find the string by searching for
date (.)+%c
but I can't think for the life of me what the replacement would be. I want to replace %c with %z (or something else later on) and keep the remaining text.
Thanks for any help.

(date.*?)%c
Replace by :$1%z

You can search for
date (.*?)%c(.*)
and replace it by
date $1%z$2
Output for your examples:
date +%m-%d%H:%M:%S%z
date +%m-%d%H%z:%M:%S
date +%z-%m-%d%H:%M:%S

Related

How to reshape timestamp in Google Sheets?

I have an imported cell in Google Sheets with the following string representing a time/date format:
2019-03-30T14:39:07-03:00
What would be the correct REGEXTRACT or DATEVALUE formula solution so that it will result in a valid Google Sheets date&time format?
The -03:00 at the end of the string can be ignored.
2019-03-30T14:39:07-03:00
should result in
yyyy-mm-dd hh:mm:ss
You can use REGEXREPLACE and use this regex,
.*?(\d{4}(?:-\d{2}){2}).(\d{2}(?::\d{2}){2}).*
And replace it with $1 $2.
Demo
I've tested and it works well in Google sheets.
Just use this,
=REGEXREPLACE(A1, ".*?(\d{4}(?:-\d{2}){2}).(\d{2}(?::\d{2}){2}).*", "$1 $2")
And replace in the cell you want to get your desired value.
Here is a screenshot showing the google sheets demo,
As you can see in the samples, this regex will find the date, no matter the date is surrounded by any optional text. In each of the case, you will have your desired date extract in the next column or any column you want.
all you need is:
=SUBSTITUTE(LEFT(A1, 19), "T", " ")
for the whole column:
=ARRAYFORMULA(SUBSTITUTE(LEFT(A1:A, 19), "T", " "))

Use RegEx to find dates and increment year by a value

I have a large number of files that contain dates. I would like to use a Regular Expression to find the dates and if possible increment the year of the date by 10.
The files can have multiple date formats ..
04/22/78
06-OCT-14
How would one write a regular expression that could find, increment, and replace the dates, or even just the year of the dates?
I plan to use a text editor like Text Pad, UltraEdit, or Notepad++ to search the files
Assuming the pattern of date is date.month.year. . in date can be any field separator.
You can use simple perl program to do this:
perl -ne 's/(\d+)$/($1+10)/e && print' filename
This will add 10 to the year, and print the date.
Output for this is:
04/22/88
06-OCT-24
Just wrote this python snippet to get it done.
import re
def add_ten_years(date):
reg = "((\d{2})(.)(\w{2,4})(.)(\d{2}))"
mat = re.search(reg, date)
if mat:
mat = mat.groups()
return ''.join(mat[1:5])+str(int(mat[5])+10)
print add_ten_years("04/22/78")
print add_ten_years("06-OCT-14")
You can configure the regex pattern to generalize it even more. Or can be easily translated to other languages. Hope it helped!

Regex exclude characters from date/time

I'm trying to convert the date/time from a message into another format but I lost track after matching the date and time.
Here's what I have:
<21>1 2014-06-06T14:32:50.010791+02:00 message etc
Here's how I cut the date and time:
(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).(\d+)+
Which produces:
2014-06-06T14:32:50.010791
Here's what I would like:
2014-06-06 14:32:50.010
Just use \d{3} instead of (\d+)+
(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).\d{3}
or
\d+-\d+-\d+T\d+:\d+:\d+.\d{3}
P.S.
To replace "T", you can use .replace("T", " ") or a similar method.
As an alternative you can group (...)T(...) and then use group[0]+" "+group[1]
It makes sense to use . instead of .

Using OpenOffice sCalc - How to use IF function with REGEX capture and if true print capture to cell

I have a worksheet (OpenOffice sCalc) with many rows of data, MOST of them have a year enclosed in ()
One of the cell's has this content: Mary had a little lamb, Sarah Josepha Hale (1830)
I would like to capture the year and save it in the cell to the right.
This stmt will tell me if a year is present:
=IF(COUNTIF(L115; ".*[(][0-9]{4,4}[)].*");"hooray"; "boo")
When I try to replace "Hooray" with $1 in this stmt I get an error:
=IF(COUNTIF(L115; ".*([(][0-9]{4,4}[)]).*");$1; "boo")
I get this: #REF!
What is the correct syntax? Thank you in advance!
Regex capturing is possible in Search/replace (must be enabled under "More Options"), but I don't know if you can use capturing in formulae.
An alternative way:
=VALUE(MID(L115;FIND("(";L115)+1;4))

Format all IP-Addresses to 3 digits

I'd like to use the search & replace dialogue in UltraEdit (Perl Compatible Regular Expressions) to format a list of IPs into a standard Format.
The list contains:
192.168.1.1
123.231.123.2
23.44.193.21
It should be formatted like this:
192.168.001.001
123.231.123.002
023.044.193.021
The RegEx from http://www.regextester.com/regular+expression+examples.html for IPv4 in the PCRE-Format is not working properly:
^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]){3}$
I'm stucked. Does anybody have a proper solution which works in UltraEdit?
Thanks in advance!
Set the regular expression engine to Perl (on the advanced section) and replace this:
(?<!\d)(\d\d?)(?!\d)
with this:
0$1
twice. That should do it.
If your input is a single IP address (per line) and nothing else (no other text), this approach will work:
I used "Replace All" with Perl style regular expressions:
Replace (?<!\d)(?=\d\d?(?=[.\s]|$))
with 0
Just replace as often as it matches. If there is other text, things will get more complicated. Maybe the "Search in Column" option is helpful here, in case you are dealing with CSV.
If this is just a one-off data cleaning job, I often just use Excel or OpenOffice Calc for this type of thing:
Open your textfile and make sure only one IP address per line.
Open Excel or whatever and goto "Data|Import External Data" and import your textfile using "." as the separator.
You should now have 4 columns in excel:
192 | 168 | 1 | 1
Right click and format each column as a number with 3 digits and leading zeroes.
In column 5 just do a string concatenation of the previous columns with a "." in between each column:
A1 & "." & B1 & "." & C1 & "." & D1
This obviously is a cheap and dirty fix and is not a programmatic way of dealing with this, but I find this sort of technique useful for cleaning up data every now and then.
I'm not sure how you can use Regular Expression in Replace With box in UltraEdit.
You can use this regular expression to find your string:
^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$