|
-
Jan 29th, 2010, 08:52 AM
#1
Thread Starter
Lively Member
Regex ending syntax
Hello,
I am trying to make a comment line reader that reads the value between a sort of syntax.
However it has an space at the end and ofc I could use the trim functions but wondering if its possible within regular expressions.
Code:
<?PHP
$var = '--- lol lol ---';
$count = preg_match_all('~---(?:[[:space:]])?(.*)[^[:space:]]$(?:[[:space:]])?---~',$var,$reg);
var_dump($reg);
?>
array
0 =>
array
0 => string '--- lol lol ---' (length=15)
1 =>
array
0 => string 'lol lol ' (length=8)
I thought something like this:
~---(?:[[:space:]])?(.*)[^[:space:]]$(?:[[:space:]])?---~
(.*)[^[:space:]]$ <- doesn't work
But it doesn't work.
Any suggestions / something to add?
Thanks in advance
-
Jan 29th, 2010, 10:36 AM
#2
Re: Regex ending syntax
.. I have no idea if you added those :space: words by yourself to illustrate that there was a space there or not, but if you did -- in the future, you should not. people who help you are going to copy and paste your code, not attempt to re-type it ;)
anyhow, I don't know what you were doing. I'm not sure if you're a super regular expression whiz or just some guy trying to get something to work. I created the following regular expression to suit your needs; I've written a quick explanation of how it works before you go ahead with using it.
PHP Code:
$match = '/^---[\s]*(.*[^\s])[\s]*---$/';
so. what this will do is look at the beginning of the string (^) and looks for three dashes. then, it looks for zero or more whitespace characters (\s), and then matches any character zero or more times that is not a whitespace character. then, it looks for zero or more whitespace characters again, followed by three dashes. this is the end of the string ($). it will only actually match the string found within the dashes, excluding any whitespace encountered on the edges of the string.
hope that works out for you.
-
Jan 29th, 2010, 04:40 PM
#3
Thread Starter
Lively Member
Re: Regex ending syntax
that helped. But run into an other problem.
now for example if I take like 3 statements.
--- comment [ something ] [ reason] time [ timestamp ] ---
it will find something ] date [ reason instead of something.
I figured cause of its only on the end of the group( (.+[^\s\[]) not ending with with ] but also cant contain a ]
any suggestions?
regex for now:
PHP Code:
$match = '~^\---[\s]*comment\[[\s]*(.+[^\s\]])[\s*\][\s]*\[[\s]*(.+[^\s\]])[\s]*\][\s]*(?:reason[\s]*\[(.+[^\s\]])[^\s]*\][\s]*)?\}$~';
Last edited by Blixa; Jan 29th, 2010 at 04:47 PM.
Reason: Adding regex
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|