Regular Expressions 101

Community Patterns

username_unix.gamesave

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^%s_([0-9]+)\\.gamesave$
/
gm

Description

while ((dir = readdir(d)) != NULL) { regmatch_t pmatch[2]; // To store the matched timestamp if (regexec(&regex, dir->d_name, 2, pmatch, 0) == 0) { // Extract the timestamp from the filename char timestamp_str[256]; int len = pmatch[1].rm_eo - pmatch[1].rm_so; strncpy(timestamp_str, dir->d_name + pmatch[1].rm_so, len); timestamp_str[len] = '\0';

        // Convert timestamp to time_t
        time_t timestamp = atol(timestamp_str);

        // Check if this is the most recent file
        if (timestamp > most_recent_time) {
            most_recent_time = timestamp;
            strncpy(most_recent_file, dir->d_name, sizeof(most_recent_file) - 1);
            most_recent_file[sizeof(most_recent_file) - 1] = '\0';
            file_found = true;
        }
Submitted by robert paulson - 4 months ago (Last modified 22 minutes ago)