正则表达式在字符串中查找问题

发布于 2024-11-17 03:57:13 字数 344 浏览 0 评论 0原文

我有一些像这样的字符串,

\r\n21.你最喜欢的宠物是什么 名字?\r\nA.猫 B.狗\r\nC.马 D.蛇\r\n22.哪个国家生产 小麦最多?\r\nA.澳大利亚 B.不丹\r\nC.印度 D.加拿大。

=======================================

现在我必须找到问题以及选择通过正则表达式的字符串。

任何人都可以提出疑问吗?

我正在像 [1-9][.] 一样解析这个问题。但我有时会遇到两个问题。

任何机构都可以提出任何改变吗?

I have some string like this,

\r\n21.what is your favourite pet
name?\r\nA.Cat B.Dog\r\nC.Horse
D.Snake\r\n22.Which country produce
wheat most?\r\nA.Australia
B.Bhutan\r\nC.India D.Canada.

=====================================

Now i have to find the questions as well as the choice from the string through regular expression.

Can anybody sujjest.

I am parsing like [1-9][.] for the question. But I am getting two questions sometimes merged.

Can any body suggest any changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫﹏色ふ单纯 2024-11-24 03:57:13
((\d+\..*?\?\\r\\n)(A\..*?)(B\..*?)(C\..*?)(D\..*?\\r\\n))

您可以使用此正则表达式,但它假设在最后一个选择之后有 \r\n 字符。

((\d+\..*?\?\\r\\n)(A\..*?)(B\..*?)(C\..*?)(D\..*?\\r\\n))

You can use this regex, but it assumes that after the last choice there are \r\n characters.

在风中等你 2024-11-24 03:57:13

我创建了两个可能的正则表达式,具体取决于您是否希望问题/答案的数字/字母出现在捕获中。

Pattern1: (?<Question>\d+\.[^?]+\?)(?:(?:\W*)(?<Answer>[ABCD]\..*?(?=$|(?:\s|\r\n)(?:[ABCD]\.|\d+\.))))*
Pattern2: \d+\.(?<Question>[^?]+\?)(?:(?:\W*)[ABCD]\.(?<Answer>.*?(?=$|(?:\s|\r\n)(?:[ABCD]\.|\d+\.))))*

我假设您希望在 C# 中使用此功能,因为您将其标记为 C#,因此这里有一些示例代码,您可以将其粘贴到新的控制台应用程序中以开始使用:

        var input = "\r\n21.what is your favourite pet name?\r\nA.Cat B.Dog\r\nC.Horse D.Snake\r\n22.Which country produce wheat most?\r\nA.Australia B.Bhutan\r\nC.India D.Canada.";
        var pattern1 = @"(?<Question>\d+\.[^?]+\?)(?:(?:\W*)(?<Answer>[ABCD]\..*?(?=$|(?:\s|\r\n)(?:[ABCD]\.|\d+\.))))*";
        var pattern2 = @"\d+\.(?<Question>[^?]+\?)(?:(?:\W*)[ABCD]\.(?<Answer>.*?(?=$|(?:\s|\r\n)(?:[ABCD]\.|\d+\.))))*";
        foreach (Match m in Regex.Matches(input, pattern2))
        {
            var question = m.Groups["Question"].Value;
            var answers = (from Capture cap in m.Groups["Answer"].Captures
                           select cap.Value).ToList();

            Console.WriteLine("Question: {0}", question);
            foreach (var answer in answers)
            {
                Console.WriteLine("Answer: {0}", answer);
            }
        }
        Console.ReadLine();

它使用正则表达式模式将每个问题解析为问题变量,并且将相关答案放入答案列表中。您可以通过更改发送到第一个 foreach 中的 Regex.Matches() 函数的模式来更改使用的模式。

I have created two possible regular expressions, depending on if you want the number/letter of the question/answer to appear in the capture or not.

Pattern1: (?<Question>\d+\.[^?]+\?)(?:(?:\W*)(?<Answer>[ABCD]\..*?(?=$|(?:\s|\r\n)(?:[ABCD]\.|\d+\.))))*
Pattern2: \d+\.(?<Question>[^?]+\?)(?:(?:\W*)[ABCD]\.(?<Answer>.*?(?=$|(?:\s|\r\n)(?:[ABCD]\.|\d+\.))))*

I am assuming you want this in C#, since you tagged it as C#, so here is some sample code you can paste into a new Console Application to begin playing with:

        var input = "\r\n21.what is your favourite pet name?\r\nA.Cat B.Dog\r\nC.Horse D.Snake\r\n22.Which country produce wheat most?\r\nA.Australia B.Bhutan\r\nC.India D.Canada.";
        var pattern1 = @"(?<Question>\d+\.[^?]+\?)(?:(?:\W*)(?<Answer>[ABCD]\..*?(?=$|(?:\s|\r\n)(?:[ABCD]\.|\d+\.))))*";
        var pattern2 = @"\d+\.(?<Question>[^?]+\?)(?:(?:\W*)[ABCD]\.(?<Answer>.*?(?=$|(?:\s|\r\n)(?:[ABCD]\.|\d+\.))))*";
        foreach (Match m in Regex.Matches(input, pattern2))
        {
            var question = m.Groups["Question"].Value;
            var answers = (from Capture cap in m.Groups["Answer"].Captures
                           select cap.Value).ToList();

            Console.WriteLine("Question: {0}", question);
            foreach (var answer in answers)
            {
                Console.WriteLine("Answer: {0}", answer);
            }
        }
        Console.ReadLine();

It uses a regex pattern to parse each question into a question variable, and the related answers into a list of answers. You can change which pattern is used by changing the pattern sent to the Regex.Matches() function in the first foreach.

捶死心动 2024-11-24 03:57:13

在 Python 中:

查找问题:

>>> import re
>>> re.findall(r'[1-9][1-9]*\.([^?]*)',s)
['what is your favourite pet name', 'Which country produce wheat most']

In Python:

Find the questions:

>>> import re
>>> re.findall(r'[1-9][1-9]*\.([^?]*)',s)
['what is your favourite pet name', 'Which country produce wheat most']
银河中√捞星星 2024-11-24 03:57:13

我不确定它是否能在孟加拉语中工作,但以下代码在英语中工作正常(至少在您提供的示例上;)):

var input = "\r\n21.what is your favourite pet name?\r\nA.Cat B.Dog\r\nC.Horse D.Snake\r\n22.Which country produce wheat most?\r\nA.Australia B.Bhutan\r\nC.India D.Canada.";

var regex = new Regex(@"(?<number>[0-9]+)\.(?<question>.+\?)\W+((?<letter>[A-Z])\.(?<answer>\w+)\W*)+");

foreach (Match question in regex.Matches(input))
{
    Console.Write("{0}. ", question.Groups["number"].Captures[0]);
    Console.WriteLine(question.Groups["question"].Captures[0]);

    foreach (Capture answer in question.Groups["answer"].Captures)
    {
        Console.WriteLine(answer.Value);
    }
}

它打印:

21. what is your favourite pet name?
Cat
Dog
Horse
Snake
22. Which country produce wheat most?
Australia
Bhutan
India
Canada

我想您可以从那里得到您需要的东西。

I'm not sure if it will work in Bengali, but the following code works OK in English (at least on the example you provided ;) ):

var input = "\r\n21.what is your favourite pet name?\r\nA.Cat B.Dog\r\nC.Horse D.Snake\r\n22.Which country produce wheat most?\r\nA.Australia B.Bhutan\r\nC.India D.Canada.";

var regex = new Regex(@"(?<number>[0-9]+)\.(?<question>.+\?)\W+((?<letter>[A-Z])\.(?<answer>\w+)\W*)+");

foreach (Match question in regex.Matches(input))
{
    Console.Write("{0}. ", question.Groups["number"].Captures[0]);
    Console.WriteLine(question.Groups["question"].Captures[0]);

    foreach (Capture answer in question.Groups["answer"].Captures)
    {
        Console.WriteLine(answer.Value);
    }
}

It prints:

21. what is your favourite pet name?
Cat
Dog
Horse
Snake
22. Which country produce wheat most?
Australia
Bhutan
India
Canada

I guess you can get what you need from there.

分分钟 2024-11-24 03:57:13

这可以帮助:

[0-9]+\.(.*?)\?\s*A\.(.*?)\s*B\.(.*?)\s*C\.(.*?)\s*D\.(.*?)\r\n

使用 \r\n 来界定问题不是一个好主意。虽然它应该适用于你的情况。

This can help:

[0-9]+\.(.*?)\?\s*A\.(.*?)\s*B\.(.*?)\s*C\.(.*?)\s*D\.(.*?)\r\n

using \r\n to delim questions is not a good idea. Though it should work in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文