Jump to content

*SOLVED* I have been learning regular expressions


redarrow

Recommended Posts

Sorry for any spellings wrong.

I have been learning regular exspressions i thort i shall shere my madness.
Good luck.




[code]
<?
echo"<br><br><br>";

//This is true dog is in the sentence.

$dog="my dog is lucky";
if(ereg("dog",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>

<?
echo"<br><br><br>";

//This is not true, the word dog not at the begining

$dog="my dog is lucky";
if(ereg("^dog",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is not true, ereg is case sencetive Dog does not match

$dog="my Dog is lucky";
if(ereg("^dog",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is true, Dog is capital letter and is at the begging

$dog="Dog is lucky";
if(ereg("^Dog",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>

<?
echo"<br><br><br>";

//This is true, eregi is not case sencetive dog does match even tho Dog upper case

$dog="my Dog is lucky";
if(eregi("dog",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is not true becouse the end $ symbol sees dog at the end

$dog="my dog is lucky";
if(ereg("dog$",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>

<?
echo"<br><br><br>";

//This is true becouse dog$ is at the end

$dog="this is my dog";
if(ereg("dog$",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>



<?
echo"<br><br><br>";

//This is not true becouse dosent see ^dog$ at the begiging and end exact

$dog="this is my dog";
if(ereg("^dog$",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>



<?
echo"<br><br><br>";

//This is true becouse ^dog$ at the begining and end exzact

$dog="dog";
if(ereg("^dog$",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is true there are three ddd in the string

$dog="dddog is mine";
if(ereg("d{3}",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is not true there are not three ddd in the string

$dog="ddog is mine";
if(ereg("d{3}",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>




<?
echo"<br><br><br>";

//This is true looking for three ddd or more

$dog="dddog is mine";
if(ereg("d{3,}",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is not true looking for 3ddd and maximum of five ddddd

$dog="dddddddog is mine";
if(ereg("d{3,5}",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>



<?
echo"<br><br><br>";

//This is true there is a d and afther the d there is a ooooooooo more then 2

$dog="dooooooooooog is mine";
if(ereg("d(o){2,}",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is false there is a d but the o issint more then 2

$dog="dog is mine";
if(ereg("d(o){2,}",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is true there is a d and a and o and this sign* means 0 or more

$dog="dog is mine";
if(ereg("d(o)*",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is not true there is a d but no s the+ sign wants one or more

$dog="dog is mine";
if(ereg("d(s)+",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>

<?
echo"<br><br><br>";

//This is true there is a d and afther that there is a o but also the+ wants to see 1 or more

$dog="dooog is mine";
if(ereg("d(o)+",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>



<?
echo"<br><br><br>";

//This is true becouse lucky is in the ereg but using the | meaning or in regular exsperisons

$dog="my dog name is lucky and we live in a house";
if(ereg("lucky|also|money",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>

<?
echo"<br><br><br>";

//This is not true no words matched in regular exsperisons using |or

$dog="my dog name is lucky and we live in a house";
if(ereg("be|also|money",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>




<?
echo"<br><br><br>";

//This is true but we are useing the brackets() to enclose the or| and also the remaining ie at the end

$dog="my dogie name is lucky  and we live in a house";
if(ereg("(dog|also|money)ie",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>



<?
echo"<br><br><br>";

//This is true but we are useing the brackets() to enclose the or| and also the remaining wildcard.e at the end the wild

//card acts as the i

$dog="my dogie name is lucky  and we live in a house";
if(ereg("(dog|also|money).e",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>

<?
echo"<br><br><br>";

//This is true but we are useing the brackets() to enclose the or| and also the remaining wildcard.e at the end the wild

//card acts as the i and adding the+ will use more then 1. wildcard

$dog="my doggie name is lucky  and we live in a house";
if(ereg("(dog|also|money).+e",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>

<?
echo"<br><br><br>";

//^.{47}$ this tell the ereg that begging^ .wildcard any charecter {47} amount of chareter and $end so if the string is 47

//chareters long true.

$dog="my doggie name is lucky  and we live in a house";
if(ereg("^.{47}$",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";
// This is true using[] brackets and og at the end

$dog="dog";
if(ereg("[dtnk]og",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is not true becouse ereg see d+o

$dog="d+o";
if(ereg("d+o",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>


<?
echo"<br><br><br>";

//This is true becouse the backslash\ makes d and o correct using a back slash

$dog="d+o";
if(ereg("d\+o",$dog)) {
echo" your dog is lucky";
}else{
echo"your dog not lucky";
}
?>
[/code]
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.