a123123 Posted August 10, 2011 Share Posted August 10, 2011 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 More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 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 https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255191 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 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 https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255194 Share on other sites More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 It works for me just fine. If it's not working then I am guessing your $_POST['link'] input is invalid. For example, I tried the string eabcd .abcd which correctly changed to eabcd abcde Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255195 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 Good for you then this title is connected with other text box, and I am sure it is correct. Where are you using this reg exp? Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255197 Share on other sites More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 Just tried it in php and changed $string=$_POST['link']; to $string='eabcde .abcd'; and it echo's out fine I'd recommend doing a var_dump on $_POST['link'] to make sure it has what you expect it does in it Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255198 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 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 https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255202 Share on other sites More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 it doesn't really matter where you have it, just put var_dump($_POST['link']); somewhere in your code and it will show its contents Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255204 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 It shows me "null". My question is, why negation with [^e] doesn't work? Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255209 Share on other sites More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 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 https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255210 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 Thanks, seems that it works partially. But it doesnt when abcd is followed by space, why? Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255216 Share on other sites More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 it should as long as there is a character before the abcd Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255217 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 everything works fine! i love u! thanks for your help!!! this space is not a big problem now, but how can I solve it? Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255221 Share on other sites More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 You could substitute the [^e] with (?<!e) which means if there's not an e before the regex Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255224 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 Good idea. How can I negate backslash character (\) in these new brackets? Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255392 Share on other sites More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 Like this? (?<![e\\]) Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255394 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 Compilation failed: missing terminating ] for character class at offset 12 interesting... what can you advice for that? Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255400 Share on other sites More sharing options...
a123123 Posted August 10, 2011 Author Share Posted August 10, 2011 Well, I found answer, its triple backslash. I think I mark it as solved and make another topic because my new problem is similar. Thanks. Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255426 Share on other sites More sharing options...
JAY6390 Posted August 10, 2011 Share Posted August 10, 2011 ah, you should actually use 4 backslashes (need to escape them both) Link to comment https://forums.phpfreaks.com/topic/244391-perl-regular-expression-doesnt-work/#findComment-1255429 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.