Jump to content

problem with inventing pattern for preg_replace


Recommended Posts

Hello. My problem is about processing Regular Expressions (Perl-Compatible)

 

as on img:

regexpsiw.jpg

 

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

 

for #1 not working:

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

 

for #2

$replace = '/\.[\w\W]+$/';
   $with = '';

preg_replace($replace,$with,$str);

(without returning correct alphanumeric findings for now, but still not working)

 

others:

'/\.[\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:

'/\s*\.[(\w)\W^\.]+?$/'  
'/\.(?:\(w?)|\W?)+$/'

$replacer = '\.$1';

 

anyone could help me with this?

 

greetings

michael

 

You should be able to do that with strpos and preg_replace in PHP.

 

<?php
$pos = strrpos($str, '.'); // this will give us the position of the last dot in the string -- strpos() will give you the first occurrence
$newstring = preg_replace("/[^a-zA-Z0-9]/", "", substr($str, 0, $pos));
// You'll have $newstring, which contains all alphanumeric characters from the beginning of $str until the last dot in the string
?>

 

Personally I would do it with strrpos() like pkSML suggests, but if you want the regular expressions solutions (the last one runs another preg_replace() inside the replacement):

 

#1

<?php
$str = 'file|  .,,  & .123^; ..asd gf@ 4& 1  *5@n%  . #{}67$ n89  ,, & |';
echo preg_replace('~(.*)\..*$~is', '$1', $str);
// file| .,, & .123^; ..asd gf@ 4& 1 *5@n% 
?>

 

#2

<?php
$str = 'file|  .,,  & .123^; ..asd gf@ 4& 1  *5@n%  . #{}67$ n89  ,, & |';
echo preg_replace('~(.*\.)(.*)$~ise', "'$1' . preg_replace('~[^0-9a-z]~i', '', '$2')", $str);
// file| .,, & .123^; ..asd gf@ 4& 1 *5@n% .67n89
?>

Woops! I thought you were just discarding everything after the last dot.

 

@thebadbad - nice to see there's another way of doing things, probably closer to 'best practice'

 

Anyhow, here's how my code changes now that I understand what you needed to happen.

 

<?php
$pos = strrpos($str, '.'); // this will give us the position of the last dot in the string -- strpos() will give you the first occurrence
$newstring = substr($str, 0, $pos) . preg_replace("/[^a-zA-Z0-9]/", "", substr($str, $pos));
// You'll have $newstring, which contains everything up to the last dot and all alphanumeric characters after the last dot in the string
?>

Thank You!  both of You enlighten me about greatly important thing, that i couldnt find in any documentation or so much commentaries, eg main php manual http://pl2.php.net/manual/pl/function.preg-replace.php - about, that PHP can also take function as replacer! ealrier it was obvious for me it was impossible.

 

Both solves are working well, but i was affraid that i would have problem to implement either two to my code, finally i turned up that indeed thebadbad #2 wouldnt make trouble as pattern with function stored in array. If I want pkSML version that looks v clear for me, i would have to change preg_replace construction.

 

<?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
?>

 

 

regexpsiw2.jpg

 

thanks for help

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.