Skip to content
Advertisement

preg_match include string with unlimit occurence

I trying to create preg_match function with a pattern to validate the future string with unlimit occurence. This is my function like this:

JavaScript

One occurrence must respect the following format any charchters between two parentheses: (mystring123/). The whole of string ($arg) is a collection of these occurrences.
For example
1-This string is valid (AAA/)(BBB/)(cc).
2-this string is not valid (AAA/)xxxx(BBB/)(cc)

The function works correctly but the pattern that I trying to create not accept more than one occurrence.

My second try, I change the pattern but the issue has been triggered when preg_match function is executed.

JavaScript

My need is how to resolve this issue, and how I can add to pattern string the followin charchters “” and “/”.

Advertisement

Answer

I’ve toiled at this task for a period of time, trying to devise a method to combine your fullstring validation with indefinite captured groups. After trying many combinations of G and lookarounds, I am afraid it cannot be done in one pass. If php allowed variable width lookbehinds, I think I could, but alas they are not available.

What I can offer is a process with the unnecessary “stuff” removed.

Code: (Demo)

JavaScript

Output:

JavaScript

Pattern #1 Breakdown:

JavaScript

Pattern #2 Breakdown:

JavaScript

Pattern #2’s effect is to locate every closing parenthesis and “explode” the string on the zero width position that follows the closing parenthesis. K ensures that no characters become casualties in the explosions.

The if condition does not need to call preg_match_all() since there can only ever be one matching string while you are validating from ^ to $. Declaring a variable to contain the “match” is pointless ( as is PREG_OFFSET_CAPTURE) — if there is a match, it will be the entire input string so just use that value if you want it.

preg_split() is a suitable substitute for a preg_match_all() call because it outputs exactly the output that you will seek in a lean single-dimensional array AND uses a very small, readable pattern. *The 3rd and 4th parameters: 0 and PREG_SPLIT_NO_EMPTY tell the function respectively that there is “no limit” to the number of explosions, and that any empty elements should be discarded (don’t make an empty element from the ) that trails cc)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement