【问题标题】:Searching using RegEx使用正则表达式搜索
【发布时间】:2019-11-04 22:36:19
【问题描述】:

我想提取数据如下:

第 11 个问题

问题:在以下代码sn-p中查找错误(如果有)进行弹出操作。 void pop() //从栈中移除一个元素 { printf(“%s”, stack[top++]); }

选项-a:运行时错误 选项-b:编译时错误 option-c:执行了pop操作,但是top移动了错误的方向 option-d:pop操作正确执行查看答案

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

我会感谢我得到的每一次帮助。

提前谢谢你

样本数据和正则表达式代码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);流行音乐(乙);推(c,s); a) abc b) b c) ac d) acb 查看答案
  9. 要执行哪些函数集才能获得以下输出?猫 a) 推(c, s);推(a,s);推(t,s);流行音乐;流行音乐;流行音乐; b) 推(c,s);流行音乐;推(a,s); pop(s);push(t,s);pop(s); c) 流行(c);流行音乐(一);弹出(t); d) 推(c,s);推(a,s);弹出(t);查看答案
  10. 如果推送“java”这个词,您的堆栈会是什么样子? a) b) c) d) 查看答案
  11. 在以下代码 sn-p 中查找错误(如果有)进行弹出操作。 void pop() //从栈中移除一个元素 { printf(“%s”, stack[top++]); } a) 运行时错误 b) 编译时错误 c) 执行了弹出操作,但是顶部移动了错误的方向 d) 正确执行了弹出操作查看答案
  12. 以下程序的输出是什么? main() { char str[]="san Foundry"; int len = strlen(str);诠释我;对于(我=0;我

【问题讨论】:

  • 始终将代码/字符串放在您的问题中。外部链接可能随时停止工作。这会让你的问题对未来的观众毫无用处。另外,请查看Markdown Editing Help,以便您可以正确标记您的问题,以便我们轻松理解它。目前,尚不清楚预期的输出是什么,以及您的问题的文本是什么。哦,它是 python 还是 JavaScript - 当涉及到正则表达式时,两个非常不同的答案(例如,JavaScript 仅部分支持lookbehinds)?
  • 为什么不直接用换行符分割(因为你的问题都不是多行的)?
  • 你的模式没有考虑到 11,或许可以试试 ^11\..*(?:\r?\n(?!12\.).*)* regex101.com/r/GgtNwZ/1 让它更有效率。
  • 另外,[a,b,c,d] 检查该位置是否存在集合 a,bcd 中的任何字符 - 而不是 abcd - 检查字符集中的每个字符。
  • 在您的问题中放置代码/字符串 -> 完成

标签: javascript python regex web-scraping


【解决方案1】:

编辑: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); push(a,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}`);
    });
}

【讨论】:

    【解决方案2】:

    问题是对于问题 10 在 a) b) 等之后没有答案。如果您也想允许这些,您可以使用 a\)\s(?&lt;a&gt;.*?\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

    【讨论】:

    • 这段代码确实选择了所有内容,并且它是选择所有内容时唯一的功能代码。问题是当我只需要选择跳过其他所有内容的问题时。问题选择应该是:在下面的代码sn-p中查找错误(如果有的话)进行pop操作。 void pop() //从栈中移除一个元素 { printf(“%s”, stack[top++]); }
    • @ИΛβΞΞĿҜHΛИ 在这种情况下,如果您不想使用数字,您可以在问题中找到特定的内容。像这样试试regex101.com/r/67e4w2/1
    猜你喜欢
    • 2011-05-20
    • 2011-09-02
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2015-02-12
    • 2021-08-06
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多