Javascript 使用正则表达式进行搜索
我想提取如下数据: 问题11 问题:在以下pop操作代码段中查找错误(如果有)。 void pop()//从堆栈中删除元素{printf(“%s”,堆栈[top++]);} 选项a:运行时错误 选项b:编译时错误 选项c:执行弹出操作,但顶部移动方向错误 选项d:正确执行pop操作查看答案 由于某种原因,下面的正则表达式停止在数字9处,并且不是从10到12的选择数字。我无法理解这个问题 我会感激得到的每一个帮助 先谢谢你 示例数据和正则表达式代码Javascript 使用正则表达式进行搜索,javascript,python,regex,web-scraping,Javascript,Python,Regex,Web Scraping,我想提取如下数据: 问题11 问题:在以下pop操作代码段中查找错误(如果有)。 void pop()//从堆栈中删除元素{printf(“%s”,堆栈[top++]);} 选项a:运行时错误 选项b:编译时错误 选项c:执行弹出操作,但顶部移动方向错误 选项d:正确执行pop操作查看答案 由于某种原因,下面的正则表达式停止在数字9处,并且不是从10到12的选择数字。我无法理解这个问题 我会感激得到的每一个帮助 先谢谢你 示例数据和正则表达式代码 ^(?\d{1,2})\.\s(?.*?\sa\
^(?\d{1,2})\.\s(?.*?\sa\\s(?.*?\sb\)\s(?.*?\s(?:c\)\s(?.*?\s(?:d\)(?.*?\s)??查看答案\s{0,3}$
const regex = /(\d{1,2}\. )|(?<=\d{1,2}\. ).*?(?=View Answer)|View Answer/gms;
const regex=/(\d{1,2}\)|(?EDIT:我认为,可以在单独的捕获组中获取所需的所有内容,包括问题编号、问题文本和“查看答案”,因此您可以替换它们(例如,使用组反向引用)或随意丢弃它们:
const regex = /(\d{1,2}\. )|(?<=\d{1,2}\. ).*?(?=View Answer)|View Answer/gms;
const regex=/(\d{1,2}\)|(问题是对于问题10在a)b)之后没有答案等等。如果您也希望允许这些,您可以使用a\)s将答案设置为可选(?问题是问题10在a)之后没有答案)b)等。如果您也希望允许这些,您可以使用a\)将答案设置为可选(?始终将代码/字符串放在你的问题中。外部链接可能在任何时候都会停止工作。这会使你的问题对未来的读者毫无用处。此外,请检查,以便你可以正确标记你的问题,以便我们能够轻松理解。目前,还不清楚预期的输出以及文本来源你的问题。哦,是python还是JavaScript?对于正则表达式,有两个截然不同的答案(例如,JavaScript仅部分支持lookbehinds)?为什么不按换行符拆分(因为你的问题都不是多行的)?你的模式不考虑11,也许可以尝试^11\*(?:\r?\n(?!12\)*)*
使它更有效率。还有[a,b,c,d]
检查集合a,bcd
中的任何字符是否存在于该位置-而不是abcd
-检查字符集中的每个字符。将代码/字符串放在问题中->不要总是将代码/字符串放在问题中。外部链接可能在任何时间点停止工作。这会使您的问题无效对未来的读者来说绝对没有用。另外,检查一下,这样你就可以正确地标记你的问题,这样我们就可以很容易地理解它。现在,还不清楚预期的输出是什么,以及你的问题中的文本是什么。哦,是python还是JavaScript?当涉及到正则表达式时,这是两个截然不同的答案(例如,JavaScript仅部分支持lookbehinds)?为什么不按换行符拆分(因为您的问题都不是多行的)?您的模式不考虑11,也许可以尝试^11\*(?:\r?\n(?!12\).*)
以提高效率。另外[a,b,c,d]
检查集合中是否有任何字符a,bcd
存在于该位置-而不是abcd
-检查字符集中的每个字符。将代码/字符串放入问题中->完成此代码确实选择了所有内容,并且在选择所有内容时它是唯一的功能代码。问题是当我只需要仅选择跳过所有其他内容的问题。问题选择应为:查找以下pop操作代码段中的错误(如果有)。void pop()//从堆栈中删除元素{printf(“%s”,堆栈[top++]);}@在这种情况下,如果你不想使用数字,你可以在问题中找到一些具体的东西。试试l
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}`);
});
}
^(?<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}$