Jump to content

[SOLVED] Simple Error Somewhere


shane18

Recommended Posts

I'm trying to make it so I can extract the Command Symbol, User Name, and Reason from a string. This will allow me to control my site w/o making a lot of pages/forms so please help.

 

<?
$CC = ".UserNameHere Reason Here";
preg_match("/^(\.|!|#|@|\+|^|\$|-|_|~)(.+?)\s?(.*)$/", $CC, $CCM);
echo "!----".$CCM[0]."-----".$CCM[1]."-----".$CCM[2]."----".$CCM[3]."----".$CCM[4]."----".$CCM[5]."----!";
?>

 

**NOTE** The Reason is optional that is why I got it optional in the regexp. **NOTE**

 

I get:

!----.TEST BLAH BLAH-----.-----T----EST BLAH BLAH------------!

Link to comment
Share on other sites

1. why didn't what i posted work... it should of went to the first space.. then posted Username, then posted Reason Here... was it because of greedyness /lazyness?

 

Yes.  Since your first .+? is lazy, and you aren't requiring the following space, and your last .* is greedy, the first .+? is satisfied by the single char match, and the last .* matches everything else.

 

2. so [^\s]+ will match until the first space then cancel?

 

Yes. Or tab or newline. 

 

 

Link to comment
Share on other sites

it effectively makes it optional.  But technically it is the same mechanics as making a lazy quantifier.  Instead of doing it on a "zero or more of this" (.*) or "one or more of this" (.+) you are doing a "one of this" (.?)

 

And even reading it like "0 or 1"  'or' is a logical condition.  Look at it in php terms:

 

if (0 or 1) { ... }

 

the condition is evaluated as true if the one on the left is true. php will not even look at the other side of that 'or'. 

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.