Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex: how to delete characters at a certain position

Status
Not open for further replies.

Cambridge10

Programmer
May 5, 2008
18
NL
I would like to create a regex which would replace any character from position six with ~1

Example:
input: This Is A Text.abc
output: Thisis~1.abc

I'm stuck on this one.
 
If you know the positions where you need the replacement to be, why use a regexp at all? The 'substr' function will give you all you need:
Code:
my $file = 'This Is A Text.abc';

substr( $file, 6, length( $file ) - 10, '~1' );
 
I didn't knew the substr function. Thanks.

Something else:
c:\x\test\filename.txt

How can I remove "c:\x\test\"?
It can be any path not a fixed value.
 
$var = 'This Is A Text.abc';
$var ~= s/^(.{6})*\.abc$/$1~.abc/gis;

I think....

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
substr() is not going to remove the spaces or alter the case like the the OP indicates in his first post:

input: This Is A Text.abc
output: Thisis~1.abc

or maybe the above example example output is not accurate.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Could the real question be: How do I transform a Win32 long filename into a DOS 8.3 format?

If that is the case, an easy solution could be:
Code:
use Win32 ;
my $LongName = 'F:\This Is A Text.abc';
my $ShortName = Win32::GetShortPathName($LongName);
print "$LongName is shortened to $ShortName\n";
which results in:
Code:
F:\This Is A Text.abc is shortened to F:\THISIS~1.ABC
where spaces have been eliminated.

Remark that the file MUST exist for this to be successful: if the file does not exist, the function returns undef

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top