Creating a regex for vowels in C

0

I need to create a regex that checks if a word contains vowels, and after creating this check, I'll cut the vowel parts of this word, but I do not know how I can create a regex in C. I saw some code like this:

int reti;
reti = regcomp(&regex, "^a[[:alnum:]]", 0);

Could I use the same example and modify it to look like this? Staying:

int reti;
reti = regcomp(&regex, "aeiou", 0);

Example:

Input : "algoritmo"

Output : "lgrtm"

How can I create a regex to do this job? Is there a C library that I can import to help me out?

    
asked by anonymous 15.06.2017 / 17:20

2 answers

1

You can do this by comparing each char of your array , like this:

int testvogal(char p);
int main(void) {
    char regex[] = "algoritmo";
    int t = 0;
    char final[99];
    for (int i=0;i<strlen(regex);i++)
    {
        if (testvogal(regex[i]) == 1){
            final[t] = regex[i];
            t++;
        }
    }
    printf(final);
}

int testvogal(char p){
    char *vogal = "aeiou";
    for (int j=0;j<strlen(vogal);j++)
    {
        if (p == vogal[j])
            return 0;
    }
    return 1;
}

See working at Ideone .

    
15.06.2017 / 22:04
1

As you said "need a regex", I thought I would only present the "pure" regex without worrying about the language, but I would have to test it somehow, so I developed it in python (I have this regex here in my "Knowledge Base" for years, is not my own), I think it will be easy to pour into the c.

Regex

regex = (?=[b-df-hj-np-tv-xz])(.)(?!)(?<!)

Explaining the parts:

(?=[b-df-hj-np-tv-xz]) Casa somente com consoantes
(.)                    A "gula" na regex, considera todas.
(?!) e (?<!)     Evita duplicidade no final

Implementation in python:

See code execution on repl.it.

import re
var = 'algoritmo'
r1 = r'(?=[b-df-hj-np-tv-xz])(.)(?!)(?<!)'
result = re.findall(r1, var)

consonants  = ''.join(str(e) for e in result)
print (consonants.split(' ')[0])
lgrtm

Actually the regex gives the result you need but not doing the way you suggest, that is, instead of identifying the vowels and removing them, it returns only the consonants.

DEMO

  

Edited Version without regex:
  I do not have time and on a very limited machine, so I developed a python version without the use of regex, I think it would be easy to convert to C if you want to try the repl I try to help.

vogais = ['a', 'e', 'i', 'o', 'u']

string = 'Algoritmo'

result = ''

# Percorre todas as letras da string 
for x in string:
    # convert p/ minúscula e verifica se esta em vogais 
    if x.lower() not in vogais:
        # se NÃO estiver em vogais, adiciona na string resultante 
        result += x

# Imprime o resultado    
print (result)
lgrtm    

View execution on repl.it.

    
15.06.2017 / 19:00