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

Link to comment
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)

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.