The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
PHP - Getting too many matches with my regex?
Discuss Getting too many matches with my regex? in the Regex Programming forum on Dev Shed. Getting too many matches with my regex? Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 23rd, 2012, 11:11 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 36 m 14 sec
Reputation Power: 0
|
|
|
PHP - Getting too many matches with my regex?
I'm fairly new with PHP and regex's. I am attempting to match the pattern: word-word 1 or more times in a file. my code:
Code:
<?php
$fIn = fopen( 'matchtext.dat', 'r' );
if( ! $fIn )
die( "Couldn't open the source file for reading<br/>");
while( $line = fgets( $fIn ) )
if( preg_match( '|\b([a-zA-Z]+-+)+[a-zA-Z]+|', $line, $matches ) ) {
echo "<pre>"; print_r( $matches ); echo "</pre>";
}
?>
is returning an additional array entry which is the next-to-the-last word in the line with a hyphen :
Array
(
[0] => learn-english-today
[1] => english-
)
Array
(
[0] => that-than-which-nothing-greater-can-be-though
[1] => be-
)
Array
(
[0] => that-than-which-nothing-greater-can-be-thought
[1] => be-
)
Where did I go wrong? I only want the first!
|

April 23rd, 2012, 11:50 PM
|
 |
Lost in code
|
|
|
|
Quote: | ... $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on. |
source
([a-zA-Z]+-+) is a parenthesized subpattern. However, the parenthesis are an essential part of your regex so you cannot remove them. If you only want the first element in the array then only use the first and just ignore the second.
|

April 24th, 2012, 12:09 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 36 m 14 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by E-Oreo If you only want the first element in the array then only use the first and just ignore the second. |
Thanks:
Do you mean the "[a-zA-Z]+|"? If so, that won't work because then it will find trailing hyphens (that-than-which-nothing-greater-can-be-)
the second part in this case needs to find a final word after the last hyphen.
|

April 25th, 2012, 02:21 AM
|
|
|
Hi, jusserfinn,
the subexpression [a-zA-Z]+-+ is captured several times. For example, in "learn-english-today", it will capture "learn-", then "english-". Only the last captured string (english-) will be saved.
If you want to match the first word, use
Code:
<?php
if( preg_match( '|\b([a-zA-Z]+)(?:-[a-zA-Z]+)+|', 'learn-english-today', $matches ) ) {
echo "<pre>"; print_r( $matches ); echo "</pre>";
}
?>
|

April 25th, 2012, 04:40 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 36 m 14 sec
Reputation Power: 0
|
|
Ok, I know there is something to be said about looking up your question in the manual (which I did), but loads more should be said about checking it twice!
PHP.net says:
Code:
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
"If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on."
So simple, I must have skimmed over it.
I fixed my code by adding [0] to the $matches in the echo statement:
Code:
<?php
$fIn = fopen( 'matchtext.dat', 'r' );
if( ! $fIn )
die( "Couldn't open the source file for reading<br/>");
while( $line = fgets( $fIn ) )
if( preg_match( '|\b([a-zA-Z]+-+)+[a-zA-Z]+|', $line, $matches ) ) {
echo "<pre>"; print_r( $matches[0] ); echo "</pre>";
}
?>
Thanks for trying though!
|

April 25th, 2012, 05:34 PM
|
 |
Lost in code
|
|
|
|
Quote: | So simple, I must have skimmed over it. |
Twice actually. Since I quoted that exact line and linked you to the manual page in my first post.
|

April 25th, 2012, 05:53 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 36 m 14 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by E-Oreo Twice actually. Since I quoted that exact line and linked you to the manual page in my first post. |
lol, Didn't even look at the quoted part, thought you were quoting me. I apologize.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|