Author Topic: RegExpReplace: replaceing nonuseable characters [solved]  (Read 1202 times)

0 Members and 1 Guest are viewing this topic.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
RegExpReplace: replaceing nonuseable characters [solved]
« on: June 20, 2009, 02:47:39 am »
Im working on a script that needs at some point to create a file with the name of a player, but the <  > | ? and such characters cannot be used in a filename. I am trying to use regexpreplace to replace any instances of the unusable characters with spaces but i cant get it to work correctly.

Code: [Select]
regexpreplace('^(/|\|:|*|?|"|>|<||) \d+',getplayerstat(ID,'name'),' ',true);

Thank You.
« Last Edit: June 20, 2009, 04:14:22 pm by Hacktank »


Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: RegExpReplace: replaceing nonuseable characters
« Reply #1 on: June 20, 2009, 05:32:04 am »
you could just get lazy and httpencode everything

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: RegExpReplace: replaceing nonuseable characters
« Reply #2 on: June 20, 2009, 05:42:55 am »
Sweet, thanks. Also I found another probelm, is it possible to rename a file that is already created through the script? Or at a last resort to delete the old file and create a new one with the new name.


Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: RegExpReplace: replaceing nonuseable characters
« Reply #3 on: June 20, 2009, 05:49:49 am »
i dont think theres a way to actually delete a file, except for shell_exec (which is disabled on most hosts). You can just write '' in it so the data is erased, but the file itself will stay... Why'd you need it anyway?

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: RegExpReplace: replaceing nonuseable characters
« Reply #4 on: June 20, 2009, 05:57:47 am »
I am making a script that will, in part, log every action of players that are in the 'watched' list and it supports IP's and names, so if they get their IP on the list and join it will create a file with their name. I need the ability to rename the file in the event that they change their name.

EDIT: Err, i just tried the httpencode and it gets rid of the bad chars but it replaces them with percent signs and letters and puts plus signs for the spaces, i guess i will just have to use a bunch of loops with getpiece.
« Last Edit: June 20, 2009, 06:06:31 am by Hacktank »


Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: RegExpReplace: replaceing nonuseable characters
« Reply #5 on: June 20, 2009, 07:57:24 am »
Easiest way to do that in regex is just remove everything that's not an alphanumeric character.

Code: [Select]
regexpreplace('[^\w]',getplayerstat(ID,'name'),'_',true); //replacing all but numalpha with underscore

You could also figure out all the characters that are not allowed. Use an escape character / before each of them (even if it's not needed, it'll be ignored then), and enclose them all in [square brackets].

Code: [Select]
regexpreplace('[/\///|/?/</>]',getplayerstat(ID,'name'),'_',true); //replacing \/|?<>  with underscore

Haven't tested the examples. I know Toumaz has a working function to change a string in a file name. It's in a private script though, so you'd have to ask him. Also, this is the wiki-page I like best for the kind of regexp that Soldat uses:
http://en.wikipedia.org/wiki/Regular_expression_examples

Then a small note on the 'swithing file names'. Think about how you are going to store all the main data (all the actions). You don't want to change the names of those files, since you'll have to copy all data then. Don't forget that players can change not only nicknames, but their IP might change as well. That means that you shouldn't store your main data in a file named after the nick or IP. AFAIK the only way to solve this is using an account system and maybe used files named after nick or IP to reference to an account.
« Last Edit: June 20, 2009, 08:04:08 am by JFK »
Come join: EliteCTF
Listen to: My Music

Offline Toumaz

  • Veteran
  • *****
  • Posts: 1904
Re: RegExpReplace: replaceing nonuseable characters
« Reply #6 on: June 20, 2009, 10:03:08 am »
Haven't tested the examples. I know Toumaz has a working function to change a string in a file name. It's in a private script though, so you'd have to ask him.
Haha, it is not in a private script; in fact it's been in the public release of Fistbox for all eternity. Plus it's really shitty. ;)

To quote the Fistbox source:
Code: [Select]
// For Windows compability; filter a filename from invalid symbols.
// Yes, modifying this to use RegExpReplace would be awesome.
function FilterFilename(const filename: string):string;
var
i,len: byte;
c: string;
begin
len:=length(filename);
result:='';
for i := 1 to len do begin
c := copy(filename,i,1);
if (c=chr(92))or(c='/')or(c=':')or(c='*')or(c='?')or(c='"')or(c='<')or(c='>')or(c='|') then
result:=result+'_'
else
result:=result+c;
end;
result:=LowerCase(result);
end;

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: RegExpReplace: replaceing nonuseable characters
« Reply #7 on: June 20, 2009, 03:50:21 pm »
Ok thanks guys, I have it working with exp.