Page 1 of 1

problem with inventing pattern for preg_replace

Posted: Tue Apr 28, 2009 2:30 pm
by s1w
Hello. My problem is about processing Regular Expressions (Perl-Compatible)

as on img:
Image

i've tried a hundreds of patterns, and still nothing

for #1 not working:

Code: Select all

'/\..+$/'
'/\..+?$/'
'/\..+?(?!\.)$/'
          \
           yes, this is followed char, I only dont know if
           logic is not inversed by $
for #2

Code: Select all

$replace = '/\.[\w\W]+$/';
   $with = '';
 
preg_replace($replace,$with,$str);
(without returning correct alphanumeric findings for now, but still not working)

others:

Code: Select all

'/\.[\w\W^\.]+$/'   
'/\.[\w\W^\.]+?$/'   
'/\.[\w\W[^\.]]+?$/'        
'/\.[\w[\W[^\.]]]+?$/'
'/\.[\w\W&&[^\.]]]+?$/'  //&& working in java, but rather not in php
           
'/\.(?:\w|\W)+$/'        //or even without []
'/\.(?:\w?|\W?)+$/'  
first idea with returnings:

Code: Select all

'/\.[(\w)\W^\.]+?$/'  
'/\.(?:\(w?)|\W?)+$/'
 
$replacer = '\.$1';
 
anyone could help me with this?

greetings
michael

Re: problem with inventing pattern for preg_replace

Posted: Tue Apr 28, 2009 2:49 pm
by prometheuzz
You mean something like this:

Code: Select all

$text = 'file| .,, & . 132^sdfhg@4rf% .#{}67$ n89 ,,& |';
echo preg_replace('/\W/', '', preg_replace('/.*\.([^.]+$)/', '$1', $text));
?

Re: problem with inventing pattern for preg_replace

Posted: Tue Apr 28, 2009 8:06 pm
by s1w
thanks, good idea... firstly i didnt know that you can put function as replacer in preg_replace like in javascript, secondly i would never quess that i can pass it also as input string ;)

here is what i have

Code: Select all

<?php
$replace = array('/[\[\{]/', '/[\]\}]/',                //change parenthesis [] {}  na ()
                 '/(.*)\.(.*)$/e',                      //format extension                                    
                 '/[^\w\s\(\)\-`\.,;\+=#&!]|'.          //allow some strange chars in filename _ -() `,.;+=!&#
                 '^[\s\.,;]*[!&\-#]+[\s,;!&\-#]*/',     //format tolerable beggining
                 '/^[\s\.,;]+|\s+(?=\s.)/');            //or cut, and finally shorten multiple spaces
$with = array('(',')',"trim('$1').'.'.preg_replace('~[\W]~','','$2')",'_','');
 
printt ('file|'.preg_replace($replace,$with,$str).'|'); //own print for debugging
?>
Image

Re: problem with inventing pattern for preg_replace

Posted: Wed Apr 29, 2009 12:32 am
by prometheuzz
What's wrong with my proposed solution?

It gives

Code: Select all

67n89
for input

Code: Select all

file| .,, & . 132^sdfhg@4rf% .#{}67$ n89 ,,& |
which are all alpha numerics after the last DOT from that string. This is what you want, right? If not, please give some example in- and output strings.