Regular expression to replace 2023-02-06T07:43:51.9732381Z with blanks - regex

I have a date stamp at the beginning of my log files, in the format 2023-02-06T07:43:51.9732381Z
There are other various dates, I have tried to use notepad ++ to write a regular expression to replace all the data formats with just blanks. I would like to have a regular expression to replace the dates.
Secondly, what if I wanted the dates to be in the format 2023-02-06T07:43:51 ?

To remove the leading date on every line do this:
Find what: ^2023[^ ]* (with trailing space)
Replace with: (empty string)
check the "Regular expression" radio button
To remove just the fractional seconds on every line do this:
Find what: ^(2023[^\.]*)[^ ]*
Replace with: $1
check the "Regular expression" radio button

The manual indicates
that Notepad++ uses "Boost" for regular expressions with its search syntax and replacement syntax.
So:
2023-02-06T07:43:51.9732381Z
could be matched at the start of lines by:
^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d[.]\d{7}Z
or more loosely by:
^\d+-\d+-\d+T\d+:\d+:\d+[.]\d+Z
To retain the part before the period, "mark" the sub-expression you want to keep:
^(\d+-\d+-\d+T\d+:\d+:\d+)[.]\d+Z
and refer to it in the replacement:
$1

Related

Regex to match the last character if it is a dot

In OpenOffice Calc how do I remove the last character if it is a dot?
Example:
dog
cat.
rabbit
It becomes:
dog
cat
rabbit
What regex would you use to obtain that?
Regular expression:
To match a ., you have to write \., because . has a special meaning in regular expressions.
To match the end of the line, you have to write $.
Putting it all together, I would use this regular expression: \.$. This will match a dot if it's at the end of the line. Demo
Removing the matched dots:
Open "Find & Replace" window by pressing Ctrl+H
Type regular expression from above into "Search for" field, this is what you want to be replaced
Leave "Replace with" field empty, as you want to replace with nothing, which means it will remove the matched content (the dots)
Under "More/Other options", turn on "Regular expressions"*
Click on "Replace" or "Replace all" button to perform the replacements
Please see documentation of this Calc feature for further details.
In OpenOffice Calc how do I remove the last character if it is a dot?
=IF(RIGHT(A1)=".";LEFT(A1;LEN(A1)-1);A1)

regular expression in notepad++

I am unable to search for the following text in notepad++, I am trying to use regular expression.
Insert into SOMETABLE (COULMN1,COULMN2,COULMN3,COULMN4,COULMN5,COULMN6) values ('ANYTHING','ANYTHING',to_date('30-APR-13 18:51:41','DD-MON-RR HH24:MI:SS'),'ANYTHING',to_date('30-APR-13 18:51:41','DD-MON-RR HH24:MI:SS'),'ANYTHING');
I am trying to search to_date('30-APR-13 18:51:41','DD-MON-RR HH24:MI:SS') from the above line.
If you want to match everything from to_date( up to the closest ), search for
to_date\([^)]*\)
(using the Perl regex option).
Explanation: [^)]* matches any number of characters except ).

Regular expression find/replace notepad++

I've a huge text file with lines like this:
080012;Bovalino;RC;CAL;0964;89034;B098;9021;http://www.website-most.en/000/000/
And i would like extract only:
080012;***Bovalino***;***RC***;CAL;***0964***;***89034***;B098;9021;http://www.website-most.en/000/000/
And delete all other text.
Can this be done with regular expressions?
You can capture the stuff you want to keep and use a backreference in the replacement string:
Find what: ^\d*;(\w*;\w*);\w*;(\d*;\d*).*
Replace with: \1;\2
And make sure you do not tick the . matches newline option.
With Notepad++ 6 you can also use $1;$2 for the replacement (with the same meaning).
If the different fields may contain all sorts of characters and not just digits and letters, this is probably your best bet:
Find what: ^[^;]*;([^;]*;[^;]*);[^;]*;([^;]*;[^;]*).*

Regular Expression to surround date with quotation marks

I have a list of data all in the same format which I need to analyse in Weka.
I need to surround the date/time values with quotation marks "" but can't work out a regular expression to complete it..
I need to change a row from this:
1028,NULL,1,21,7,AD9,06A,60136859,NULL,1,4,3,2012-02-21 10:05:00.100,2012-02-21 10:05:23.170
to a row like this:
1027,NULL,1,21,7,AD9,06A,60136859,NULL,1,5,4,"2012-02-21 10:03:53.643","2012-02-21 10:04:29.787"
where the date/time values are surrounded by quotation marks.
This will work in notepad++ as long as your datetime values are always fully formatted.
Find what: (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})
Replace with: "\1"
This works because of backreferences. Everything that is captured within parenthesis is stored as a backreference. You access backreferences by typing \number where number correlates to the position of parenthesis in the regex. So since we are only using one pair of parenthesis, want backreference 1, and we use \1.
So you find the entire date and it gets stored in \1 because of the parenthesis in your regex. Then you replace the entire date with "entire date" aka "\1".

Replace leading spaces with Notepad++

I'd like to use Notepad++ to replace all leading spaces on a line with a like number of given characters. So for instance, I want to change:
zero
one
two
three
into:
zero
#one
##two
###three
I haven't been successful at getting this working. I did find Regex to replace html whitespace and leading whitespace in notepad++, but wasn't able to get the result I wanted.
Is this possible with Notepad++? I'd rather not have to write code to do this...
As Tim's answer indicates, this can't be done in a single search/replace, however here is how you can accomplish the same task fairly quickly using multiple replacements:
Find: ^( *)[ ]
Replace with: \1#
Now just spam the "Replace All" button until it indicates that there were no matches to replace. This will replace a single space at the beginning of each line on each click, so it will require the same number of clicks as your most-indented line.
Make sure "Regular expression" is selected as the search mode.
You would need variable-length lookbehind assertions to do this in a single regex, and Notepad++ doesn't support these.
For the record, in EditPadPro you can search for (?<=^ *)\s and replace with #.