Filename without extension

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
JasonGH
Posts: 7
Joined: Sat Dec 07, 2013 12:24 am
Location: VA, USA

Filename without extension

Post by JasonGH »

Hi,
How can I copy file name from each line and paste it at the beginning of the line with regular expression. For example, I have directory file listing as follows:

C:\Users\abcxyz\Documents\Default.rdp
C:\Users\abcxyz\Documents\desktop.ini
C:\Users\abcxyz\Documents\LotusInstall.log
C:\Users\abcxyz\Documents\eFax Messenger 4.4\Fax 1.efx
C:\Users\abcxyz\Documents\eFax Messenger 4.4\Fax 2.efx
C:\Users\abcxyz\Documents\eFax Messenger 4.4\Fax 3.efx
C:\Users\abcxyz\Documents\eFax Messenger 4.4\Fax 4.efx
C:\Users\abcxyz\Documents\eFax Messenger 4.4\faxed.gif

and the new listing should look like this:

Default-C:\Users\abcxyz\Documents\Default.rdp
desktop-C:\Users\abcxyz\Documents\desktop.ini
LotusInstall-C:\Users\abcxyz\Documents\LotusInstall.log
Fax 1-C:\Users\abcxyz\Documents\eFax Messenger 4.4\Fax 1.efx
Fax 2-C:\Users\abcxyz\Documents\eFax Messenger 4.4\Fax 2.efx
Fax 3-C:\Users\abcxyz\Documents\eFax Messenger 4.4\Fax 3.efx
Fax 4-C:\Users\abcxyz\Documents\eFax Messenger 4.4\Fax 4.efx
faxed-C:\Users\abcxyz\Documents\eFax Messenger 4.4\faxed.gif

Basically, I need to copy a string from the last backward slash "\" to the last dot "." and paste at the beginning of each line.

Any insight is really appreciated.
Thanks Guys.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Find what: ^(.+\\(.+)\.)
Replace with: $2-$1

[X] Regular expression
^(.+\\(.+)\.) matches

Code: Select all

^           the beginning of a line
(           start of captured text number 1
  .+          any non-empty string within a line (see below)
  \\          a literal back-slash
  (           start of captured text number 2
    .+          any non-empty string within a line (see below)
  )           end of captured text number 2
  \.          a literal dot
)           end of captured text number 1
where .+ matches:

Code: Select all

.           any character except newline
+           ... any non-zero number of times
The text matched by the entire regex is replaced with

Code: Select all

$2          captured text number 2
-           a literal hyphen
$1          captured text number 1
JasonGH
Posts: 7
Joined: Sat Dec 07, 2013 12:24 am
Location: VA, USA

Post by JasonGH »

Actually replace with \2-\1.

I never think about nested group in RE.

You are genius Ben!!!
Thank you so much.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

In TextPad 7 you can use $2-$1 or \2-\1. In earlier versions, with TextPad's old and much weaker regex engine, you must use \2-\1.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

You can do it without nested groups. In TextPad 7:
Find what: ^(.+)\\(.+)\.
Replace with: $2-$1\\$2.
Post Reply