How to keep the first 8 characters of a name but leave the rest of the Name intact?

How would I trim the first 8 characters in a name but leave the rest of the note intact?
Problem: I have many old notes from nvAlt that I imported into Tinderbox that had this format:

20210630080005 This is a note about Nothing

I want to create a stamp to change it to
20210630 This is a note about nothing

I must admit, I have not done many text manipulations in Tinderbox. SubString() looked like it might work to get the first 8 characters BUT…I also want the rest of the name as well.

Here is where I am getting stuck. Here is the code for the first 8 characters, but I am not sure of the correct code I need to leave the rest of the note intact.

Wrong
$Name=$Name.substr(0,8);

Want:
20210630 This is a note about Nothing

Thanks
Tom

OK, I fixed the thread name as it was describing a different outcome to the one you want.

So, you have a note that is essentially ‘yyyymmddhhmmss Some name....’ and you want it as ’ yyyymmdd Some name....'. IOW, you want to lose the 6 digits of the time part of the date/time sequence.

The problem here is we can’t just look for and replace a 6-digit string as that can match anywhere in the initial sequence. We could target 6-digits+space and replace with a space:

$Name = $Name.replace("\d{6} "," ");

This works but gives a false positing if the textual part of the name has that sequence. Thus ‘20210630080005 This is a note about #123456 Nothing’ would become ‘20210630 This is a note about # Nothing’.

Happily, regex are precise. Back-references to the rescue:

$Name = $Name.replace("(^\d{8})\d{6}(.*)","$1$2");

Here we:

  • match and capture the beginning of the $Name (first 8 digits) as back reference $1 (i.e. ‘20210630’)
  • match the bit we don’t want and make no back-reference
  • match and capture the rest of the $Name as back reference $2 (i.e. ’ This is a note about Nothing’ - note the space retained at the beginning)
  • replace the existing $Name with the combination of $1 and $2, i.e. ‘20210630 This is a note about Nothing’)
  • use that string as the new $Name value
  • tada!

More on back-references. Yes, back-references are very confusing at first sight, especially if one is new to regex. I don’t think there is an easy explanation, or they’d be easier to understand. :slight_smile: But read the docs (and looks for deeper documentation of regex back-references online) and with a bit of practice you’ll be using them with confidence—or at least with some sense of what to try if they don’t work first time.

Ahhhh…brilliant. I knew I was missing a part of the puzzle! I did not even think of back-references in regex. So obvious after you point it out.

Thanks again for all your assistance
Tom

1 Like

Wouldn’t it be easier to do it with /substr()?

$Part1=$Name.substr(0,8);
$Part2=$Name.substr(14);
$Name=$Part1+$Part2;

1 Like