c - 使用 ANSI C 中的正则表达式扫描和交换字符串值

标签 c regex swap ansi-c

我想在我的 C 程序中转换给定的输入,例如:

foo_bar_something-like_this

进入此:

thissomethingbarfoolike

说明:

每次我得到 _ 时,以下文本都会一直到(但不包括)下一个 _- (或结尾)该行)需要转到开头(并且需要删除前面的 _)。每次我得到 - 时,以下文本都会一直到(但不包括)下一个 _- (或行尾) ) 需要附加到末尾(删除 -)。

如果可能的话,我想使用正则表达式来实现这一点。如果有一种方法可以直接从标准输入执行此操作,那将是最佳选择。

请注意,不必在单个正则表达式中执行此操作。我可以做某种循环来做到这一点。在这种情况下,我相信我必须首先捕获变量中的数据,然后执行我的算法。

我必须对输入中的每一行执行此操作,每行以 \n 结尾。

编辑:我已经为此编写了一段代码,没有使用任何与正则表达式相关的内容,此外我应该首先发布它,我很抱歉。我知道不应该使用 scanf 来防止缓冲区溢出,但字符串在程序中使用之前已经经过验证。代码如下:

#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 100001 //A fixed maximum amount of characters per line
int main(){
  char c=0;
  /*
  *home: 1 (append to the start), 0 (append to the end)
  *str: array of words appended to the begining
  *strlen: length of str
  *line: string of words appended to the end
  *linelen: length of line
  *word: word between a combination of symbols - and _
  *wordlen: length of the actual word
  */
  int home,strlen,linelen,wordlen;
  char **str,*line,*word;
  str=(char**)malloc(MAX_LENGTH*sizeof(char*));
  while(c!=EOF && scanf("%c",&c)!=EOF){
    line=(char*)malloc(MAX_LENGTH);
    word=(char*)malloc(MAX_LENGTH);
    line[0]=word[0]='\0';
    home=strlen=linelen=wordlen=0;
    while(c!='\n'){
      if(c=='-'){ //put word in str and restart word to '\0'
        home=1;
        str[strlen++]=word;
        word=(char*)malloc(MAX_LENGTH);
        wordlen=0;
        word[0]='\0';
      }else if(c=='_'){ //put word in str and restart word to '\0'
        home=0;
        str[strlen++]=word;
        word=(char*)malloc(MAX_LENGTH);
        wordlen=0;
        word[0]='\0';
      }else if(home){ //append the c to word
        word[wordlen++]=c;
        word[wordlen]='\0';
      }else{ //append c to line
        line[linelen++]=c;
        line[linelen]='\0';
      }
      scanf("%c",&c); //scan the next character
    }
    printf("%s",word); //print the last word
    free(word);
    while(strlen--){ //print each word stored in the array
      printf("%s",str[strlen]);
      free(str[strlen]);
    }
    printf("%s\n",line); //print the text appended to the end
    free(line);
  }
  return 0;
}

最佳答案

我认为正则表达式不能满足您的要求,因此我用 C 编写了一个简单的状态机解决方案。

//
//Discription: This Program takes a string of character input, and parses it
//using underscore and hyphen as queue to either send data to
//the begining or end of the output.
//
//Date: 11/18/2017
//
//Author: Elizabeth Harasymiw
//

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100

typedef enum{ AppendEnd, AppendBegin } State; //Used to track either writeing to begining or end of output

int main(int argc,char**argv){
        char ch;                   //Used to hold the character currently looking at
        State state=AppendEnd;     //creates the State
        char Buffer[MAX_SIZE]={};  //Current Ouput
        char Word[MAX_SIZE]={};    //Pending data to the Buffer
        char *c;                   //Used to index and clear Word
        while((ch = getc(stdin)) != EOF){
                if(ch=='\n')continue;
                switch(state){
                        case AppendEnd:
                                if( ch == '-' )
                                        break;
                                if( ch == '_'){
                                        state = AppendBegin;     //Change State
                                        strcat(Buffer, Word);    //Add Word to end of Output
                                        for(c=Word;*c;c++)*c=0;  //Clear Word
                                        break;
                                }
                                {
                                        int postion = -1;
                                        while(Word[++postion]);  //Find end of Word
                                        Word[postion] = ch;      //Add Character to end of Word
                                }
                                break;
                        case AppendBegin:
                                if( ch == '-' ){
                                        state = AppendEnd;       //Change State
                                        strcat(Word, Buffer);    //Add Output to end of Word
                                        strcpy(Buffer, Word);    //Move Output from Word back to Output
                                        for(c=Word;*c;c++)*c=0;  //Clear Word
                                        break;
                                }
                                if( ch == '_'){
                                        strcat(Word, Buffer);    //Add Output to end of Word
                                        strcpy(Buffer, Word);    //Move Output from Word back to Output
                                        for(c=Word;*c;c++)*c=0;  //Clear Word
                                        break;
                                }
                                {
                                        int postion = -1;
                                        while(Word[++postion]);  //Find end of Word
                                        Word[postion] = ch;      //Add Character to end of Word
                                }
                                break;

                }
        }
        switch(state){ //Finish adding the Last Word Buffer to Output
                case AppendEnd:
                        strcat(Buffer, Word); //Add Word to end of Output
                        break;
                case AppendBegin:
                        strcat(Word, Buffer); //Add Output to end of Word
                        strcpy(Buffer, Word); //Move Output from Word back to Output
                        break;
        }

        printf("%s\n", Buffer);
}

关于c - 使用 ANSI C 中的正则表达式扫描和交换字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371287/

相关文章:

regex - 如何用空格替换行内制表符,保持对齐?

c++ - 交换算法赋予所有相同的值

c - 测试功能时出现未知解析错误

c++ - 如何创建 C API 到 C++ 函数

c - 从 fgets 字符数组中删除空格

php - mysql交换项目与行号

c# - 变量改变...另一个变量?

c - 获取是否收到信号

regex - bash 正则表达式 "find anything between last slash and dot"

c# - 正则表达式跳过模式