Jump to content

Perl regular expression doesn't work


a123123

Recommended Posts

Hello. I am using code:

<?php
$patterns = array('/[^e]abcd/');
$replacements = array('abcde');
$string=$_POST['link'];
echo preg_replace($patterns, $replacements, $string);
?>

I am using array because there is more patterns, but I place one as example. This code should change to 'abcde' every string 'abcd' not followed by 'e', but it doesn't. I am doing everything the same as its described in internet, why my code doesn't work then? Thanks for help :)

 

BTW, I think you have mistake in verify question: "John, George, Paul and..." and when I wrote "fischer" it didn't applied, so I had to refresh site.

Link to comment
https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/
Share on other sites

Firstly, I have no idea what you are on about with the verification and fischer. I'm guessing the answer is Ringo (the beatles)

 

As for your regex, it is trying to match any "abcd" that has any character before it that isn't an e

So it will match

7abcd

3abcd

aabcd

The only thing it won't match is

eabcd

Firstly, I have no idea what you are on about with the verification and fischer. I'm guessing the answer is Ringo (the beatles)

http://en.wikipedia.org/wiki/John_Fischer_(painter)

the beatles? i heard this word, but didn't know what it means, nevermind ;) already solved

 

As for your regex, it is trying to match any "abcd" that has any character before it that isn't an e

So it will match

7abcd

3abcd

aabcd

The only thing it won't match is

eabcd

Yes, I know this, and that what I want. But by code doesnt work - why?

Just tried it in php and changed

$string=$_POST['link'];

to

$string='eabcde .abcd';

<?php
$patterns = array('/[^e]abcd/');
$replacements = array('abcde');
$string='abcd eabcd';
echo preg_replace($patterns, $replacements, $string);
?>

doesn't work...

I'd recommend doing a var_dump on $_POST['link'] to make sure it has what you expect it does in it

Where I should place var_dump then? I need to have rather $_POST function that defined string because it should be text box.

ok, so the problem is your input, not the regex

The regex needs to match all of the pattern, not just the [^e]

since abcd isn't in the pattern, it doesn't match or replace anything

You need to sort out your input form to get this working, your php code and regex is fine

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.