0

i have a group of numbers that start with string, inside a string in C:

intr 250727985 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 238463729 0 0 0 0 0 8510 1009565 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 75963 0 0 0 0 0 0 0 0 0 0 0 0 0 6416543 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 29812 197 0 0 0 0 842664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

i want to capture (match) only the 26th position of them (desired match: 1009565 on my return.)

i've tried this pattern:

(?:[0-9]+[[:space:]]){26}(?<![0-9])

but this is capturing whole string until the desired position. How to achieve this with RegExp in C? someone could provide an sample source?

RegExp is the fastest (and lightest on system resources) way to do this? i need to repeat this operation various times in a second, all the uptime.

i'm confused on how to do this.

2 Answers2

2

I believe using RegExp will make the problem more complicated, as suggested by others, using strtok is much easier.

You can parse the string at each space, and match the pattern you are trying to search for using strcmp from <string.h>.

Here is a basic idea of what you might want to use:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *string_search(char *string, unsigned position);

int main(void) {
    char string[] = "250727985 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 238463729 0 0 0 0 0 8510 1009565 0 0 0 0 0";
    char *check;
    unsigned position = 26;

    check = string_search(string, position);

    if (check != NULL) {
        printf("String %s found in position %d.\n", check, position);
    } else {
        printf("No string found in position %d.\n", position);
    }

    return 0;
}

char *string_search(char *string, unsigned position) {
    char *number;
    const char *delim = " ";
    unsigned pos_count = 1;

    number = strtok(string, delim);
    while (number != NULL) {
        if (pos_count == position) {
            return number;
        }
        pos_count++;
        number = strtok(NULL, delim);
    }
    return NULL;
}  
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

The Unix strings library has strtok which divides a string into tokens given a delimiter. You should be able to simply iterate through the string until you get to the position you want.

A discussion on Stack Overflow of strtok is at Using strtok in c which has some sample code and gotchas.

Community
  • 1
  • 1
vielmetti
  • 1,864
  • 16
  • 23
  • always i've had searched about regexp in certain scenarios (like formatting XML and or HTML content, filtering some kind of data) i've read how this could be imprecise or could even fail sometimes, so i'm in this kind of situation again – eduardogarcia234 Dec 28 '16 at 03:48
  • and of course, thank you for your feedback, i'll look onto it – eduardogarcia234 Dec 28 '16 at 03:52