Match pattern is a function in validation transform. It is used to match the input strings. This function can be used to compare alphabets (a-z, A-Z), numbers (0-9) and special characters.
Match_pattern cannot be used to match sub-strings.
Syntax
match_pattern(input_string,pattern_string)
Here,
input_string is the string to be matched. It could be alphabets, numbers, etc.
pattern_string is the pattern that you want to find in the whole string.
Return value
The return value for this function is 0 or 1.
If the return value is 1 then the input string matches.
If the return value is 0 then the input string does not match.
The below table shows the examples with patterns:
Pattern with examples |
Use |
Result |
print(match_pattern(‘Janani’, ‘Xxxxxx’)); |
x – Used for lowercase alphabets. |
Return value:1 |
print(match_pattern(‘JANANI’, ‘Xxxxxx’)); print(match_pattern(‘JANANI’, ‘XXXXXX’)); |
X – Used for uppercase letters. |
Return value: 0 Return value:1 |
print(Match_pattern(‘Jeni Krish’, ‘Xxxx Xxxxx’)); |
Return value:1 |
|
print(Match_pattern(123,999)); |
9 – Used for numbers |
Return value:1 |
print(match_pattern(‘jeni4′,’jeni[!\3]’)); print(match_pattern(‘jeni3′,’jeni[!\3]’)); |
\ – Escape character. It is used to avoid a number specifically. |
Return value:1 since number 3 is not found in the string. Return value: 0 since number 3 is found in the string. |
print(match_pattern(‘janani’,’*’)); |
*- Used for characters appearing 0 or more times. |
Return value:1 |
print(match_pattern(‘a1′,’a?’)); print(match_pattern(‘a1sdf’,’a?’)); |
? — Characters occurring one and only once |
Return value: 1 since after the character a only one character should appear. Return value: 0 since after the character a many characters appear. |
print(match_pattern(‘a1′,’a[123]’)); print(match_pattern(‘a4′,’a[123]’)); |
[ ]– Characters occurring inside the braces only one time. |
Return value: 1 since character 1 is in the list of pattern string. Return value: 0 since character 4 is not in the list of pattern string. |
print(match_pattern(‘Akash’ , ‘[!A]’ )); |
[!]–Any character but not the characters that appears after the exclamation point. Eg: (i.e. [!AB] can allow any, say Name, that does not start with a A or B. |
Return value: 0 since string starting with alphabet A should be avoided. |
Informative….
Nice but worth saying match_regex() is much more powerful!
match_regex() is basically the same function but whereas match_pattern() returns a 1 or a 0, match_regex() returns the matched value. Both have their place and are both helpful. match_pattern() would probably be better used in validation.
I think you will find that, unlike many other languages, match_regex() also returns an integer 0 / 1 indicating failure / success. One thing to note is that match_regex() matches against the whole string not just any location in the string, it is as if it wraps the ^ and $ anchors around the input regular expression.
Yup, my bad. Agreed, match_regex() is much more powerful. Anything where you can use regular expressions over basic searches is preferable in my opinion.
Helpful…Thank You….. Keep writing..
very helpful..
Hi guys, I got one question:
I want to check, if an input stringĀ has a ‘P’ as 4. character. Is there a way to do it with match pattern?
I.e. something like:
match_pattern(‘AFZPK7190K’, [A-Z][A-Z][A-Z][P][A-Z][A-Z][0-9][0-9][0-9][0-9][A-Z])
Thanks