ajoo Posted February 6, 2018 Share Posted February 6, 2018 (edited) Hi all, I want to know if it's possible that the regex matches for the exact subject string, instead of checking for the match anywhere inside of a string. to take an example, if the regex is /([A-z0-9])\w+\.\w{3,4}/ and the subject string is , (i.e. the one within the double quotes.) "'sjvjhvbj.bnm'lsjksnkboubouboubsoubsobob.txt" then because the string begin's with a single quote, it should return a mismatch. That is what I would like to achieve. What is happening is that it finds a pattern match inside the string as : sjvjhvbj.bnm which is not what is desired. Thanks all ! Edited February 6, 2018 by ajoo Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted February 6, 2018 Solution Share Posted February 6, 2018 I'm really not following your request. Are you only wanting to return a match if the match starts at the very beginning of the string? If so, then you just need to add the start of line anchor: '^' /^([A-z0-9])\w+\.\w{3,4}/ 1 Quote Link to comment Share on other sites More sharing options...
ajoo Posted February 6, 2018 Author Share Posted February 6, 2018 Hi psycho ! Thanks for the response ! Yes you understood it right. That was almost what i wanted. I also wanted that it should give a mismatch if there is any character in the subject string which is not allowed by the regex. The following seems exactly right so far i could test. /^([A-z0-9])\w+\.\w{3,4}$/ If you can verify that I'ld be grateful. One more thing, even though it seems not required because of the regex now takes care of it, what if i wanted to ensure that there can be only one dot in the string? How could that be achieved. Thanks loads ! Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 7, 2018 Share Posted February 7, 2018 No offense, but you should do your own testing. Create an array of possible inputs. Have some that should return a match but may "stretch" the requirements and create some input that should fail the match for various reasons (invalid characters, multiple periods, etc. etc.). Then loop through the array and verify the results. You can start with this. function testMatch($input) { if(preg_match("/^([A-z0-9])\w+\.\w{3,4}$/", $input, $match)) { return $match[0]; } return false; } $testValues = array( array("'sjvjhvbj.bnm'lsjksnkboubouboubsoubsobob.txt", 'Fail: Starts with quotemark'), array("abcd.ab!", 'Fail: Only two valid characters after period'), array("abcdefghi!", 'Fail: No period'), array("abcd.abc!", 'Pass'), array("abcd.abcd!", 'Pass'), array("abcd.abcde!", 'Pass') //Create multiple test cases to cover whatever scenarios are needed ); //Run the tests foreach($testValues as $test) { $value = $test[0]; $expected = $test[1]; $return = testMatch($value); if($return==false) { $result = "<span style='color:red;'>Fail</span>"; } else { $result = "<span style='color:red;'>Pass ({$return})</span>"; } //Output the result echo "<b>Input:</b> {$value}, <b>Expected:</b> {$expected}, <b>Result:</b> {$result}<br>\n"; } Quote Link to comment Share on other sites More sharing options...
ajoo Posted February 7, 2018 Author Share Posted February 7, 2018 (edited) Hi Psycho ! Thanks for the reply. Actually I tested it online here with a whole bunch of strings and it seemed ok. I was just trying to be doubly sure and so asked for your affirmation. Thanks very much for the code snippet. These, by the way, array("abcd.abc!", 'Pass'), array("abcd.abcd!", 'Pass'), array("abcd.abcde!", 'Pass') failed when i tested them online as I would want them too. I will check them using the snippet as well. Thanks. Edited February 7, 2018 by ajoo Quote Link to comment Share on other sites More sharing options...
ajoo Posted February 7, 2018 Author Share Posted February 7, 2018 Hi, Yea so it fails there as well. Great ! Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.