Re: Experiences with match() subexpressions?

Liste des GroupesRevenir à cl awk 
Sujet : Re: Experiences with match() subexpressions?
De : mortonspam (at) *nospam* gmail.com (Ed Morton)
Groupes : comp.lang.awk
Date : 13. Apr 2025, 18:52:27
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtgtkr$3br8e$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 4/10/2025 8:07 PM, Ed Morton wrote:
On 4/10/2025 2:09 AM, Janis Papanagnou wrote:
On 10.04.2025 09:06, Janis Papanagnou wrote:
I'm looking for subexpressions of regexp-matches using GNU Awk's
third parameter of match(). For example
>
   data = "R=r1,R=r2,R=r3,E=e"
   match (data, /^(R=([^,]+),){2,5}E=(.+)$/, arr)
>
The result stored in 'arr' seems to be determined by the static
parenthesis structure, so with the pattern repetition {2,5} only
the last matched data in the subexpression (r3) seems to persist
in arr. - I suppose there's no cute way to achieve what I wanted?
>
To clarify; what I wanted is access of the values "r1", "r2", "r3",
and "e" through 'arr'.
 Correct, you can't do what you want using just `match()`, it's simply matching a regexp with capture groups against a string, just like sed does.
 There are, of course, several other ways to get `arr[]` populated the way you want. e.g split(), patsplit(), while(match()), or dynamically generating the regexp. The best one to choose will depend on the real values that r1, etc. can have, for example it'd be hard to use split() if `r1` can be a quoted string that might itself contain similar substrings such as `data = "R=\"R=r1,R=r2\",R=r2,R=r3,E=e"`.
FWIW, probably more for the benefit of any awk newcomers reading this, if your data really could have quoted fields (otherwise a simple `split(data,",")` is all you need) then, assuming they follow the same quoting rules as for CSVs, I'd use either of these or similar with GNU awk (for `patsplit()`:
     data = "R=\"R=r1,R=r2\",R=r2,R=r3,E=e"
     nf = patsplit(data, arr, /[RE]=([^,]*|"([^"]|"")*")/)
     delete arr
     for ( i in arr ) {
         sub(/[^=]+=/, "", arr[i])
     }
or any awk:
     data = "R=\"R=r1,R=r2\",R=r2,R=r3,E=e"
     nf = 0
     delete arr
     while ( match(data, /[RE]=([^,]*|"([^"]|"")*")/, a) ) {
         arr[++nf] = substr(data, RSTART+2, RLENGTH-2)
         data = substr(data, RSTART+RLENGTH)
     }
either of which would populate `arr[]` with:
     "R=r1,R=r2"
     r2
     r3
     e
and set `nf` to the number of entries in `arr[]`.
Regards,
     Ed.

Date Sujet#  Auteur
10 Apr 25 * Experiences with match() subexpressions?22Janis Papanagnou
10 Apr 25 `* Re: Experiences with match() subexpressions?21Janis Papanagnou
10 Apr 25  +* Re: Experiences with match() subexpressions?14Kenny McCormack
10 Apr 25  i`* Re: Experiences with match() subexpressions?13Janis Papanagnou
10 Apr 25  i `* Re: Experiences with match() subexpressions?12Kenny McCormack
10 Apr 25  i  `* Re: Experiences with match() subexpressions?11Janis Papanagnou
11 Apr 25  i   `* Re: Experiences with match() subexpressions?10Aharon Robbins
11 Apr 25  i    +* Re: Experiences with match() subexpressions?5Janis Papanagnou
11 Apr 25  i    i+- Re: Experiences with match() subexpressions?1Kaz Kylheku
18 Apr 25  i    i`* Re: Experiences with match() subexpressions?3Manuel Collado
18 Apr 25  i    i +- Re: Experiences with match() subexpressions?1Kenny McCormack
18 Apr 25  i    i `- Re: Experiences with match() subexpressions?1Janis Papanagnou
11 Apr 25  i    +- Re: Experiences with match() subexpressions?1Kaz Kylheku
11 Apr 25  i    +* The new matcher (Was: Experiences with match() subexpressions?)2Kenny McCormack
11 Apr 25  i    i`- Re: The new matcher (Was: Experiences with match() subexpressions?)1Janis Papanagnou
11 Apr 25  i    `- Re: Experiences with match() subexpressions?1Kaz Kylheku
11 Apr 25  `* Re: Experiences with match() subexpressions?6Ed Morton
13 Apr 25   `* Re: Experiences with match() subexpressions?5Ed Morton
14 Apr 25    `* Nitpicking the code (Was: Experiences with match() subexpressions?)4Kenny McCormack
14 Apr 25     `* Re: Nitpicking the code (Was: Experiences with match() subexpressions?)3Janis Papanagnou
15 Apr 25      `* Re: Nitpicking the code (Was: Experiences with match() subexpressions?)2Ed Morton
15 Apr 25       `- Re: Nitpicking the code (Was: Experiences with match() subexpressions?)1Janis Papanagnou

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal