0

I have a large text file which contains many timestamps. The timestamps look like this: 2013/11/14 06:52:38AM. I need to remove the last two characters (am/pm/AM/PM) from each of these. The problem is that a simple find and replace of "AM" may remove text from other parts of the file (which contains a lot of other text).

I have done a find using the regular expression (:\d\d[ap]m), which in the above example would track down the last bit of the timestamp: :38AM. I now need to replace this with :38, but I don't know how this is done (allowing for any combination of two digits after the colon).

Any help would be much appreciated.

EDIT: What I needed was to replace (:\d\d)[ap]m with \1

jonnybolton
  • 487
  • 4
  • 7

2 Answers2

1

Make (:\d\d[ap]m) into (:\d\d)[ap]m and use $1 not \1

Noam Rathaus
  • 5,405
  • 2
  • 28
  • 37
  • Thanks for the response. It works using `\1`. Is there any advantage to using `$1` instead? Or is it just something that Notepad++ does differently? – jonnybolton Nov 27 '13 at 12:47
  • I always used $1 and not \1, I believe $1 is perl way? and \1 is ruby? or python? anyway if both work, cool :) btw, notepad++ officially supports PCRE (Perl), and documentation states ``$n, ${n}, \n Returns what matched the subexpression numbered n. Negative indices are not alowed.`` – Noam Rathaus Nov 27 '13 at 12:51
  • In the case of Notepad++ `\1` and `$1` makes no difference. But in most other cases, like Perl, PHP etc, it does so it's better to get used to the `$` notation. Read why on [perlre](http://perldoc.perl.org/perlre.html#Warning-on-\1-Instead-of-$1). – psxls Nov 27 '13 at 12:55
0

Go to Search > Replace menu (shortcut CTRL+H) and do the following:

  1. Find what:

    [0-9]{2}\K[AP]M
    
  2. Replace:

    [leave empty]
    
  3. Select radio button "Regular Expression"

  4. Then press Replace All

You can test it at regex101.

Note: the use of [0-9] is generally better than \d (read why), and avoiding to use a capture group $1 with the use of \K is considered better. It's definitely not important in your case, but it is good to know :)

Community
  • 1
  • 1
psxls
  • 6,807
  • 6
  • 30
  • 50