Internal Error Matching Pattern Array Against Value
Sign in Pricing Blog Support Search GitHub This repository Watch 2 Star 8 Fork 8 opensolutions/OSS-Framework Code Issues 1 Pull requests 0 Projects 0 Wiki Pulse Graphs Permalink Branch: master Switch branches/tags Branches Tags gh-pages master Nothing to show v1.0.0 Nothing to show Find file Copy path OSS-Framework/src/OSS/Validate/OSSNotRegex.php Fetching contributors… Cannot retrieve contributors at this time Raw Blame History 151 lines (135 sloc) 3.97 KB * @author The
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow http://stackoverflow.com/questions/18754096/matching-values-in-string-array the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of http://permalink.gmane.org/gmane.comp.php.zend.framework.scm/18946 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Matching values in string array up vote 10 down vote favorite 4 Problem: Looking for a more efficient internal error way of finding whether there is an exact matching value in a 1d array -- essentially a boolean true/false. Am I overlooking something obvious? Or am I simply using the wrong data structure, by using an array when I probably should be using a collection object or a dictionary? In the latter I could check the .Contains or .Exists method, respectively In Excel I can check for a value in a vector internal error matching array like: If Not IsError(Application.Match(strSearch, varToSearch, False)) Then ' Do stuff End If This returns an exact match index, obviously subject to limitations of Match function which only finds the first matching value in this context. This is a commonly used method, and one that I have been using for a long time, too. This is satisfactory enough for Excel -- but what about other applications? In other applications, I can do basically the same thing but requires enabling reference to the Excel object library, and then: If Not IsError(Excel.Application.match(...)) That seems silly, though, and is difficult to manage on distributed files because of permissions/trust center/etc. I have tried to use the Filter() function: If Not Ubound(Filter(varToSearch, strSearch)) = -1 Then 'do stuff End If But the problem with this approach is that Filter returns an array of partial matches, rather than an array of exact matches. (I have no idea why it would be useful to return substring/partial matches.) The other alternative is to literally iterate over each value in the array (this also is very commonly used I think) -- which seems even more needlessly cumbersome than calling on Excel's Match function. For each v in vArray If v = strSearch Then ' do stuff End If Next arrays vba excel-vba data-structures
Zend_Validate_Regex: Newsgroups: gmane.comp.php.zend.framework.scm Date: Friday 19th March 2010 19:45:26 UTC (over 6 years ago) http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> http://www.w3.org/1999/xhtml"> [framework-svn][21572] standard/branches/release-1.10: [GENERIC] Zend_Validate_Regex: Revision 21572 Author thomas Date 2010-03-19 12:45:26 -0700 (Fri, 19 Mar 2010) Log Message [GENERIC] Zend_Validate_Regex: - merged r21571 to branch Modified Paths standard/branches/release-1.10/library/Zend/Validate/Regex.php standard/branches/release-1.10/tests/Zend/Validate/RegexTest.php Diff Modified: standard/branches/release-1.10/library/Zend/Validate/Regex.php (21571 => 21572) --- standard/branches/release-1.10/library/Zend/Validate/Regex.php 2010-03-19 19:43:01 UTC (rev 21571) +++ standard/branches/release-1.10/library/Zend/Validate/Regex.php 2010-03-19 19:45:26 UTC (rev 21572) @@ -34,6 +34,7 @@ { const INVALID = 'regexInvalid'; const NOT_MATCH = 'regexNotMatch'; + const ERROROUS = 'regexErrorous'; /** * @var array @@ -41,6 +42,7 @@ protected $_messageTemplates = array( self::INVALID => "Invalid type given, value should be string, integer or float", self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'", + self::ERROROUS => "There was an error while using the pattern '%pattern%'", ); /** @@ -61,6 +63,7 @@ * Sets validator options * * @param string|Zend_Config $pattern + * @throws Zend_Validate_Exception On missing 'pattern' parameter * @return void */ public function __construct($pattern) @@ -95,11 +98,19 @@ * Sets the pattern option * * @param string $pattern + * @throws Zend_Validate_Exception if there is a fatal error in pattern matching * @return Zend_Validate_Regex Provides a fluent interface */ public function setPattern($pattern) { $this->_pattern = (string) $pattern; + $status = @preg_match($this->_pattern, "Test"); + + if (false === $status) { + require_once 'Zend/Validate/Exception.php'; + throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'"); + } + return $this; } @@ -109,7 +120,6 @@ * Returns true if and only if $value matches against the pattern option * * @param string $value - * @throws Zend_Validate_Exception if there is a fatal error in pattern matching * @return boolean */ public function isValid($value) @@ -123,14 +133,15 @@ $sta