Match regex

Liebes Forum,

ich versuche gerade im 4. und 5. Parameter der Funktion Position und Länge aller Vorkommen eines Musters zu erhalten. Bei meinem Versuchen scheint die Suche immer nach dem ersten Fund abzubrechen -> Ergebnisarrys haben die Länge 1. Vielleicht hat jemand ein funktionierendes Beispiel?

Mein Beispiel zur Suche von 7-stelligen Zahlen sieht folgendermaßen aus:

<code 4D>
$tnrRegex:="([0-9]{7}"+")"
//$tnrRegex:="[0-9]{7}"+""
ARRAY LONGINT($ar_pos;0)
ARRAY LONGINT($ar_length;0)
$found:=Match regex($tnrRegex;“1234567 1234567 1234567”;1;$ar_pos;$ar_length)

</code 4D>

Hi Johannes,
I hope you won’t mind a reply in english.
The goal of using arrays parameters in match regex is to collect “sub pattern(s) inside the main pattern”. As your pattern has no sub patterns (limited by parenthesis), the use of this syntax with arrays ($ar_pos;$ar_length) has no sense.

With this example, you’ll get how it works:
<code 4D>
$str_t:=“1234567 8901234 5678901 2345678”
$regex_t:="(\d{7}) (\d{7})"
//main pattern is 2 groups of 7 digits separated by a space
//inside this main pattern we will capture each group of 7 digits
$start_t:=1
while(Match regex($regex_t;$str_t;$start_t;$ar_pos;$ar_length))
$main_t:=Substring($str_t;$start_t;$ar_pos{0};$ar_length{0}) //main
$group1_t:=Substring($str_t;$start_t;$ar_pos{1};$ar_length{1}) //capture group1
$group2_t:=Substring($str_t;$start_t;$ar_pos{2};$ar_length{2}) //capture group2
$start_t:=$ar_pos{0}+$ar_length{0}+1 //next
end while
</code 4D>

Dear Arnaud,

Thank you very much. I wanted to use match regex to find all the occurrences of my pattern without using a loop, but if I understand your answer right, that is not possible with this command.

The solution is a loop.

I wish you and the forum a happy new year.

Es wird QUERY BY FORMULA verwendet um der Job zu erledingen

A very good year too :slight_smile:

: Johannes LEIDHEISER

The solution is a loop.
Yes, think Match regex as Position, it is a “low level / basis” function. But if you don’t want to re invent the wheel (the loop…), 2 components are based on that function:
http://forums.4d.fr/Post/FR/1633598/1/5021337#1635297
http://forums.4d.fr/Post/FR/16241963/1/18433879#18433879

Hi,

As Arnaud said, using arrays for pos and length is necessary when you are using regex with “capturing groups” (search regex and capturing groups with google to see what it is about).

You can use regex without capturing groups, and you are right, for repeated patterns, you need a while loop.

<code 4D>
ARRAY TEXT($tt_match;0)
C_TEXT($vt_regex;$vt_text)
$vt_regex:="[0-9]{7}"
$vt_text:=“1234567 1234567 1234567”
C_LONGINT($vl_from)
$vl_from:=1 // usually 1
If(Length($vt_text)>=$vl_from) // 4D regex does not like starting at pos 1 when string is empty
C_LONGINT($vl_pos;$vl_length)
While (Match regex($vt_regex;$vt_text;$vl_from;$vl_pos;$vl_length))
APPEND TO ARRAY($tt_match;Substring($vt_text;$vl_pos;$vl_length))
$vl_from:=$vl_pos+$vl_length
End while
End if
</code 4D>

Arnaud,

I think it should be this :

<code 4D>
$start_t:=$ar_pos{0}+$ar_length{0}//next

</code 4D>

Instead of this :
<code 4D>
$start_t:=$ar_pos{0}+$ar_length{0}+1//next

</code 4D>

PS : happy new year 2017 !!!

Also,

Regex and test cases are good friends :slight_smile:

I recommand to build test cases for your regex to prove them right :wink:

HTH

The components Arnaud mentioned seem usefull. But in my simple case it was easier to do the while loop.

Test cases are really neccessary for regex :-).
https://regex101.com/ provides a nice GUI for experiments.

Hi,

: Johannes LEIDHEISER

Test cases are really neccessary for regex Smile.
https://regex101.com/ provides a nice GUI for experiments.

Nice, but 4D regex engine is from ICU library if I am not mistaken.
http://userguide.icu-project.org/strings/regexp
I can behave marginally differently than other “engines”…So be aware of this when testing on other platforms/engines…

Even if it’s not the same regex engine as 4d, I often start with textWrangler, then copy to 4d (if someone knows if it’s possible and how to display capture groups, BTW…)

: Bruno LEGAY

PS : happy new year 2017 !!!
Ein Gutes Neues Jahr! :smiley: