problem with inventing pattern for preg_replace

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
s1w
Forum Newbie
Posts: 2
Joined: Tue Apr 28, 2009 2:04 pm

problem with inventing pattern for preg_replace

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: problem with inventing pattern for preg_replace

Post 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));
?
s1w
Forum Newbie
Posts: 2
Joined: Tue Apr 28, 2009 2:04 pm

Re: problem with inventing pattern for preg_replace

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: problem with inventing pattern for preg_replace

Post 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.
Post Reply