Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I want to match a string using REGEX which follows the syntax:
Text/number
So my preg_match() function is...
preg_match()
if(preg_match("/[^A-Za-z/0-9]$/ ", $folio)) $err[] = "Wrong value, it's should be lik: C/455";
But getting error message...
Try this:
$folio = "Text/15"; if(preg_match('~[a-z]/[\d]~i', $folio)) echo "match"; else echo "no match";
Add a comment
~
You needed to escape / using \. Also numbers is a subset of text and you need to include it in your text part. You need one or more text/numeric characters, so a + is required.
/
\
+
It adds up to the following statement:
if(preg_match("/^[A-Za-z0-9]+\/[0-9]+$/", $folio))
You either need to escape / or use different char to surround regexp, for example:
if(preg_match("@[^A-Za-z/0-9]$@ ", $folio))
if ( ! preg_match('/\b[a-z]+\/[0-9]+\b/i', $folio)) { $err[] = "Wrong value, it's should be like this: C/455"; }
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.