使用RegEx搜索

我要提取数据如下:

第11个问题

问题:在以下用于弹出操作的代码段中找到错误(如果有)。 void pop()//从堆栈中删除元素{printf(“%s”,stack [top ++]); }

选项-a:运行时错误 选项b:编译时错误 选项-c:执行弹出操作,但顶部移动方向错误 选项-d:弹出操作正确执行查看答案

下面的正则表达式出于某种原因停在第9位,而不是从10到12的选择数字。我无法解决问题

我会很感激我得到的每一个帮助。

提前谢谢

样本数据和RegEx代码RegEx and Sample Data

^(?<number>\d{1,2})\.\s(?<question>.*?)\sa\)\s(?<a>.*?)\sb\)\s(?<b>.*?)\s(?:c\)\s(?<c>.*?)\s(?:d\)(?<d>.*?)\s)?)?View Answer\s{0,3}$
  1. 使用堆栈反转单词可用于查找给定单词是否是回文。 a)正确b)错误的观点答案
  2. 哪个是最适合单词反转的数据结构? a)队列b)堆栈c)树d)图查看答案
  3. 使用堆栈反转单词或字符串所需的操作是push()和pop()。 a)正确b)错误的观点答案
  4. 使用堆栈算法反转单词的时间复杂度是多少? a)O(N log N)b)O(N2)c)O(N)d)O(M log N)查看答案
  5. 如果使用堆栈将“ abbcabb”一词反转,将得到什么词? a)bbabbca b)abbcabb c)bbacbba d)bbacabb查看答案
  6. 逆向单词算法需要多少个堆栈? a)一b)两c)三d)四查看答案
  7. 如果弹出给定的堆栈会出现什么结果? a)拍拍b)点击c)atp d)apt查看答案
  8. 如果执行以下操作序列,将输出什么?推(a,s);推(b,s); Pop(b);推(c,s); a)abc b)b c)ac d)acb查看答案
  9. 要获取以下输出将执行哪些功能?目录a)push(c,s);推(a,s);推(t,s);流行音乐;流行音乐;流行音乐; b)推(c,s);流行音乐;推(a,s); pop(s); push(t,s); pop(s); c)pop(c); pop(a); pop(t); d)推(c,s);推(a,s); pop(t);查看答案
  10. 如果推入“ java”一词,您的堆栈看起来如何? a)b)c)d)查看答案
  11. 在以下用于流行操作的代码段中找到错误(如果有)。 void pop()//从堆栈中删除元素{printf(“%s”,stack [top ++]); } a)运行时错误b)编译时错误c)执行了弹出操作,但顶部朝错误的方向移动d)正确执行了弹出操作查看答案
  12. 以下程序的输出是什么? main(){char str [] =“ san Foundry”; int len = strlen(str);我对于(i = 0; i
yunmengyao1_ 回答:使用RegEx搜索

编辑:This one为您提供所需的一切,我认为-在问题编号,问题文本和“查看答案”的单独捕获组中,因此您可以将其替换(例如,使用组反向引用)或随意丢弃它们:

const regex = /(\d{1,2}\. )|(?<=\d{1,2}\. ).*?(?=View Answer)|View Answer/gms;

This regex works,如果您不介意不匹配“查看答案”,我也将单元测试保存在Regex101中。

const regex = /\d{1,2}.*?(?=View Answer)/gms;

const str = `9. What are the set of functions that are to be executed to get the following output? cat a) push(c,s); push(a,s); push(t,s); pop(s); pop(s); pop(s); b) push(c,s); pop(s); push(a,s); pop(s);push(t,s);pop(s); c) pop(c ); pop(a); pop(t); d) push(c,s); pop(t); View Answer
10. How will your stack look like if the word ‘java’ is pushed? a) b) c) d) View Answer
11. Find the error (if any) in the following code snippet for pop operation.
void pop() //removing an element from a stack { printf(“%s”,stack[top++]); }
a) run time error b) compile time error c) pop operation is performed,but top moved in wrong direction d) pop operation is performed properly View Answer
12. What will be the output of the following program?
main() { char str[]="san foundry"; int len = strlen(str); int i;   for(i=0;i<len;i++) push(str[i]); // pushes an element into stack   for(i=0;i<len;i++) pop(); //pops an element from the stack }
a) sanfoundry b) san foundry c) yrdnuof nas d) foundry nas View Answer
Sanfoundry Global Education & Learning Series – Data Structure.
To practice all areas of Data Structure,here is complete set of 1000+ Multiple Choice Questions and Answers.`;

let m;

while ((m = regex.exec(str)) !== null) {

    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    m.forEach((match,groupIndex) => {
        console.log(`Found match,group ${groupIndex}: ${match}`);
    });
}
,

问题在于,对于问题10 ,在a)b)等之后没有答案。如果您也希望允许这些答案,则可以使用a\)\s(?<a>.*?\s)?将答案设为可选并将空白移到可选组中,以防止必须匹配2个空白字符。

问题11 中的问题是void pop() //removing an element from a stack { printf(“%s”,stack[top++]); }换了一行。

要解决此问题,您可以添加一个可选的非捕获组(?:\r?\n.*)?来匹配第二行(如果有的话)。

要匹配所有行和组,您的模式可能类似于:

^(?<number>\d{1,2})\.\s(?<question>.*?(?:\r?\n.*)?)\sa\)\s(?<a>.*?\s)?b\)\s(?<b>.*?\s)?c\)\s(?<c>.*?\s)?d\)(?<d>.*?\s)?View Answer\s{0,3}$

Regex demo

本文链接:https://www.f2er.com/3163318.html

大家都在问