Jump to content

an exact string match only


Go to solution Solved by Psycho,

Recommended Posts

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 by ajoo
Link to comment
https://forums.phpfreaks.com/topic/306447-an-exact-string-match-only/
Share on other sites

  • Solution

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}/
  • Like 1

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 !

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";
}

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 by ajoo
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.