Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:40:53+00:00 2026-06-09T06:40:53+00:00

I’m working on some code that needs to serialize Perl regexes, including any regex

  • 0

I’m working on some code that needs to serialize Perl regexes, including any regex flags. Only a subset of flags are supported, so I need to detect when unsupported flags like /u are in the regex object.

The current version of the code does this:

static void serialize_regex_flags(buffer *buf, SV *sv) {
  char flags[] = {0,0,0,0,0,0};
  unsigned int i = 0, f = 0;
  STRLEN string_length;
  char *string = SvPV(sv, string_length);

Then manually processes string char-by-char to find flags.

The problem here is that the stringification of regex flags changed (I think in Perl 5.14) from e.g. (?i-xsm:foo) to (?^i:foo), which makes parsing a pain.

I could check the version of perl, or just write the parser to handle both cases, but something tells me there must be a superior method of introspection available.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:40:55+00:00Added an answer on June 9, 2026 at 6:40 am

    In Perl, you’d use re::regexp_pattern.

     my $re = qr/foo/i;
     my ($pat, $mods) = re::regexp_pattern($re);
     say $pat;   # foo
     say $mods;  # i
    

    As you can see from the source of regexp_pattern, there’s no function in the API to obtain that information, so I recommend that you call that function too from XS too.

    perlcall covers calling Perl functions from C. I came up with the following untested code:

    /* Calls re::regexp_pattern to extract the pattern
     * and flags from a compiled regex.
     *
     * When re isn't a compiled regex, returns false,
     * and *pat_ptr and *flags_ptr are set to NULL.
     *
     * The caller must free() *pat_ptr and *flags_ptr.
     */
    

    static int regexp_pattern(char ** pat_ptr, char ** flags_ptr, SV * re) {
       dSP;
       int count;
       ENTER;
       SAVETMPS;
       PUSHMARK(SP);
       XPUSHs(re);
       PUTBACK;
       count = call_pv("re::regexp_pattern", G_ARRAY);
       SPAGAIN;
    
       if (count == 2) {
          /* Pop last one first. */
          SV * flags_sv = POPs;
          SV * pat_sv   = POPs;
    
          /* XXX Assumes no NUL in pattern */
          char * pat   = SvPVutf8_nolen(pat_sv); 
          char * flags = SvPVutf8_nolen(flags_sv);
    
          *pat_ptr   = strdup(pat);
          *flags_ptr = strdup(flags);
       } else {
          *pat_ptr   = NULL;
          *flags_ptr = NULL;
       }
    
       PUTBACK;
       FREETMPS;
       LEAVE;
    
       return *pat_ptr != NULL;
    }
    

    Usage:

    SV * re = ...;
    
    char * pat;
    char * flags;
    regexp_pattern(&pat, &flags, re);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.