PCRE_GET_SUBSTRING(3)

✨ PCRE_GET_SUBSTRING ✨

okay bestie, welcome to PCRE_GET_SUBSTRING(3) πŸ’– this one lives in the Library Functions Manual and it's the little glow-up of the PCRE family β€” aka Perl-compatible regular expressions πŸ¦‹


🌸 SYNOPSIS 🌸

#include <pcre.h>

int pcre_get_substring(const char *subject, int *ovector,
     int stringcount, int stringnumber,
     const char **stringptr);

int pcre16_get_substring(PCRE_SPTR16 subject, int *ovector,
     int stringcount, int stringnumber,
     PCRE_SPTR16 *stringptr);

int pcre32_get_substring(PCRE_SPTR32 subject, int *ovector,
     int stringcount, int stringnumber,
     PCRE_SPTR32 *stringptr);

πŸŽ€ DESCRIPTION πŸŽ€

so this is a convenience function for extracting a captured substring πŸ’… like, you already matched, you already got your capture, and now you just want the actual text out of it. no cap, that's the whole vibe.

the arguments are, in order:

  • subject β€” the subject that has been successfully matched ✨
  • ovector β€” the offset vector that pcre[16|32]_exec() used πŸ‘‘
  • stringcount β€” the value returned by pcre[16|32]_exec() 🌈
  • stringnumber β€” the number of the required substring πŸ“
  • stringptr β€” where to put the string pointer πŸŽ€

the memory the substring gets placed in? that's obtained by calling pcre[16|32]_malloc() 🫢 and listen, when you're done with it, the convenience function pcre[16|32]_free_substring() can be used to free it. free your girl, she deserves it πŸ’–

the yield of the function β€” the return, the moment of truth β€” is the length of the substring ⭐ OR PCRE_ERROR_NOMEMORY if sufficient memory could not be obtained 😭 OR PCRE_ERROR_NOSUBSTRING if the string number is invalid πŸ’€

fr fr: invalid string number gets you PCRE_ERROR_NOSUBSTRING, and not having enough memory gets you PCRE_ERROR_NOMEMORY. those are the two ways this can say "no bestie" πŸ™ˆ

also there's a complete description of the PCRE native API in the pcreapi page, and a description of the POSIX API in the pcreposix page β€” go read those if you want the full lore πŸ“š


mwah πŸ’œ that's the page!! this one's from PCRE 8.30, dated 24 June 2012, and the manual name is PCRE_GET_SUBSTRING(3) β€” slay, go capture something iconic bestie βœ¨πŸ‘‘