Creating a regex for vowels in C

Asked

Viewed 756 times

0

I need to create a regex check if a word contains vowels, and after creating this check, I will cut out the vowel parts of this word, but I don’t know how to create a regex in C. I saw some codes so :

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

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

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

Example :

Input : "algoritmo"

Output : "lgrtm"

How can I create a regex to do this job ? There is some library in C, which I can import for help in case ?

  • Use of regex is mandatory?

  • It is not, but I thought it would be better to use, would have another way to do this ? For example, using an if that checks each vowel, and then cut this part of the word ?

  • I tried to do some things but I did not get any considerable result. See here.

  • And if you change that regex[i] = '.' to something else, it wouldn’t work ?

  • Yes, you can change the '.' by any other character.

  • I need to somehow make the final output normal, not with extra spaces or characters.

Show 1 more comment

2 answers

1

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

Regex

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

Explaining the parts:

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

Python implementation:

See the code execution in repl.it.

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

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

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

DEMO


Edited Version without regex:
I’m not in 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    

See the execution in repl.it.

  • This part of for and in result, would it be a foreach ? If it is, there are ways I can put it easier in C.

  • Yes, actually the result is an object list (matrix), thus ['l', 'g','r','t','m'] the line of ''.join(str(e) for e in result) just pull each rudder from the matrix to form the string. In other languages it can be totally different, as you "resolve" a regex in c? It may even be q the result already comes ready.

  • From what I know regex in C is much more complex, it’s not enough to create a list of what you’re going to check and that’s it, and there’s no library to mess with it. You have to import them elsewhere, so it’s harder than in Python.

  • I edited the answer and put a version without the use of regex, now go. :-)

  • Which version of c vc is using?

  • I got a way to do it, I’m writing my answer.

  • I gave +1 why you gave some interesting ideas too, thank you.

Show 2 more comments

1


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

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 on Ideone.

  • This is my version (in python), converted to c, Ahahaha!

  • @Sidon I didn’t even see right how you did it, I took what I had done before as base kk

  • No stress! But that’s my version, yeah. Ahahaha!

  • Hello Francisco, thanks for the code, I would like to take the user input and test the vowels with this code ?

  • Use the function scanf. See working on Ideone. If the answer is right, don’t forget to mark it as correct!

  • Thank you, this is the way I really wanted it. I marked it as the correct answer and +1.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.