moving year in a date from the end to the front.
-
Hello
I’m new to this program but i have a large document with many dates like this “23032022”, and i want it to be like this: “20222303” - I hope there’s is anyone that could help me here?
-
A possible problem is if your data has other 8-digit numbers that aren’t dates, but if it does just let us know and we’ll adjust to that. Another assumption to make is that your dates are not adjacent to other alphanumeric data, e.g., you don’t have
mydate23032022
.Keeping it simple for now… :
Find:
(\d{2}\d{2})(\d{4})
Replace:${2}${1}
Search mode: Regular expression -
Hi I had a similar problem and the solution I adopted was crating a some macros to perform the conversion in many direction. Here they are:
<Macro name="Date convert all dd-/.mm-/.yyyy → yyyy-mm-dd" Ctrl="yes" Alt="no" Shift="yes" Key="89"> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(\d{2})(\.|-|/)(\d{2})(\.|-|/)(\d{4})" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam="($5)-($3)-($1)" /> <Action type="3" message="1702" wParam="0" lParam="768" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> </Macro> <Macro name="Date convert dd.mm.yyyy → yyyy-mm-dd" Ctrl="no" Alt="yes" Shift="yes" Key="68"> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(\d{2})(\.)(\d{2})(\.)(\d{4})" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam="($5)-($3)-($1)" /> <Action type="3" message="1702" wParam="0" lParam="768" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> </Macro> <Macro name="Date convert yyyy-mm-dd → dd.mm.yyyy" Ctrl="yes" Alt="yes" Shift="yes" Key="89"> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(\d{4})(-)(\d{2})(-)(\d{2})" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam="($5).($3).($1)" /> <Action type="3" message="1702" wParam="0" lParam="768" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> </Macro>
Notice that they are made to match my standard date format that is yyyy-mm-dd. So if needed can be freely adapted to specific use cases.
-
Or, if you want to rearrange day and month as well you could use
Search:(\d{2})(\d{2})(\d{4})
Replace:${3}${2}${1}
And if you like to learn about RegExes look here
-
@gerdb42 Thanks for this:) worked perfectly :)
-
@gerdb42 Hello again, I have another problem. in a large document i have some lines like this: “-4 497” and like this: “17 787” i want to remove the space? do you know how?
-
Hello, @Vegard-Johansen, @gerdb42, @alan-kilborn, @wonkawilly and All,
I think that a correct answer could be :
-
SEARCH
(?<=\d)\x20+(?=\d)
-
REPLACE
Leave EMPTY
Best Regards,
guy038
-
-
@guy038 Im sorry but i didnt get that to work :(
-
Did you make sure Search Mode was set to Regular expression?
Maybe show what you did here.
-
@Alan-Kilborn Hello
I have a large document that look like this:
“120000” “various” “646958” “20220323” “12” “firstname lastname” “-4 497” “20220407” “646958”
And i want to remove the space in: “-4 497”
-
@Vegard-Johansen Are the double quotes part of your input? Or did you put them in as a clarification of your question?
In general, it is clearest and easiest when you cut and paste the relevant part of your actual input into a codeblock (triggered by </>).
-
@Paul-Wormer Hello
Its part of the input :)
-
Find: "(-?\d+)\x20(\d+)" Replace: "\1\2"
-
@Paul-Wormer did not work :( maybe i do something wrong :(
-
Hello, @Vegard-Johansen, @gerdb42, @alan-kilborn, @wonkawilly, @paul-wormer and All,
May be I should have given more details on my regex S/R !
So :
-
Start N++ and open your document
-
Open the Replace dialog (
Ctrl + H
) -
Uncheck all box options
-
Enter
(?<=\d)\x20+(?=\d)
in the Find what : zone -
Verify that the Replace with : zone is
EMPTY
-
Check the
Wrap around
box -
Select the
Regular expression
search mode -
Click on the
Replace All
button
=> All the
Space
characters, which exist within numbers, should have disappeared !- Save the changes
Best Regards,
guy038
-
-
@guy038 said in moving year in a date from the end to the front.:
(?<=\d)\x20+(?=\d)
sorry but i cant understand what im doing wrong… But i cant get it to work :I
-
@Vegard-Johansen Did you include the double quotes in both the Find expression and the Replace expression? Did you have exactly one space between the digits? Did you check the regex mode box?
My regex is a sort of brute force solution, I capture quotes and digits before and after a single space and copy them without a space in between them. @guy038 has a more sophisticated solution. He matches one (or more) space(s) that are surrounded by one digit on the left and one digit on the right and replaces the matching space(s) by an empty string.
However, both methods work. I have tried them both.
-
@Vegard-Johansen said in moving year in a date from the end to the front.:
i cant understand what im doing wrong
Could you give us a screenshot of your “Replace” dialog when you’re trying to use one of those regular expressions? We can probably figure out what setting you forgot to include.
-
-
Your Replace dialog looked right, so I went to your original post and (using my moderator powers) looked at the text you typed rather than what the forum rendered. When I copied from there and pasted into Notepad++, I saw
Please notice that the character between the
-4
and the497
is not a space; it is the special characterNBSP
(Non-breaking space). Because you said “i want to remove the space”, and did not accurately tell us what character was between there, we could not derive a regex that works. The\x20
in the regular expression only applies to the normal ASCII space character, not to a tab or to a NBSP or to any other space-like character.If your Notepad++ is not showing the
NBSP
symbol, then make sure you are on a relatively-recent Notepad++ version (v8.5 or newer), and make sure that View > Show Symbol > Show Non-printing Characters is turned on (has a checkmark next to it):
But back to your question:
Since we now know it’s a non-breaking space, we can change @guy038’s regular expression to
(?<=\d)\xA0+(?=\d)
, and that will match just if there’s a non-breaking space between those characters. If there might be any horizontal space (real space, tab, nbsp, or others), then use(?<=\d)\h+(?=\d)
instead, to make it match on any horizontal space.This shows the importance of knowing (and sharing) what the actual characters are.