我想用正则表达式来格式化文本

时间:2018-01-17 08:27:23

标签: regex notepad++

我非常感谢在记事本++中完成以下任务的任何帮助。

我有超过40,000行,如下。所有这些都是英语语言测试。它们看起来像这样:

Q1 "I know that you don't like seafood but our friends make the best seafood Fettuccini Alfredo I have ever had.
      Will you agree to keep an ......... mind and try it before deciding you don't like it?" Jill asked her son.

  (a) open  (b) airy    (c) indifferent (d) ignorant

Q2 The boss was a scary guy. When he called you into his office, you could bet that you would receive the worse
     insults you have ever had to endure and there was something about him that would stop anyone from talking
     back to him. People immediately froze in their ......... and meekly walked into his office when he called them.

  (a) paths (b) tracks  (c) cars    (d) shoes

Q3 In ......... to change the sink, he would have to turn off the water that runs to the facet. He failed to do so and got

      a surprise when water started liberally spraying down on the kitchen floor.

   (a) ability  (b) possibility (c) plausibility    (d) order

Q4 Since the company put a set of sexual harassment rules in ......... incidents of sexual harassment were virtually

      non-existent.

   (a) ordering (b) place   (c) storage (d) foundation

Q5 "Your shed is in pretty poor ......... . The back of the foundation is sinking and there is water getting into it from

      the roof. I can't help you with the foundation but we can look for ways to seal it," Rob said to Christian.

  (a) mass  (b) density (c) support (d) shape

正如你所看到的那些问题不在一行中,但它们被一个输入加一个空行加上一些空格字符打破了。

我想实现这样的目标:

Q1 "I know that you don't like seafood but our friends make the best seafood Fettuccini Alfredo I have ever had. Will you agree to keep an ......... mind and try it before deciding you don't like it?" Jill asked her son.

  (a) open  (b) airy    (c) indifferent (d) ignorant

Q2 The boss was a scary guy. When he called you into his office, you could bet that you would receive the worse insults you have ever had to endure and there was something about him that would stop anyone from talking back to him. People immediately froze in their ......... and meekly walked into his office when he called them.

  (a) paths (b) tracks  (c) cars    (d) shoes

Q3 In ......... to change the sink, he would have to turn off the water that runs to the facet. He failed to do so and got a surprise when water started liberally spraying down on the kitchen floor.

   (a) ability  (b) possibility (c) plausibility    (d) order

Q4 Since the company put a set of sexual harassment rules in ......... incidents of sexual harassment were virtually non-existent.

   (a) ordering (b) place   (c) storage (d) foundation

Q5 "Your shed is in pretty poor ......... . The back of the foundation is sinking and there is water getting into it from the roof. I can't help you with the foundation but we can look for ways to seal it," Rob said to Christian.

  (a) mass  (b) density (c) support (d) shape

所以我需要一行中的所有问题,只要一个问题,直到问题结束为止。问题选项必须在它们之下,我认为我不需要仅仅改变问题选项(a,b,c,d)。

手动,我必须逐行删除并删除字符,直到每个问题都是一行。有成千上万的问题,这将是一件困难的事情。有没有办法可以在带有正则表达式的Notepad ++中完成?

如果有帮助,每个问题都从Q1,Q2,Q3开始,直到Q10为止。所有以(a)开头的行都是问题选项。

3 个答案:

答案 0 :(得分:2)

两种方法:

根据事实,您想要附加的行的开头始终缩进,您可以使用

\R++\h++([^(])

并替换为 $1

或者基于您不想合并以开头括号或Q号开头的行的事实,您可以使用

\R++\h*+((?!Q\d)[^(])  

再次替换为 $1

答案 1 :(得分:1)

您可以使用以下正则表达式:

Q(.*)(\r?\n)+\h+(\w)

和替换:

Q\1 \3

Q(.+)\v+\h+(\w)

和替换:

Q\1 \2

点击Replace All几次,即可完成。

<强>说明

  • Q(.+)\v+\h+(\w)将选择以Q开头的所有行,后跟一个或多个字符,并以一个或多个EOL / Carriage return字符结尾,后跟几个水平空格字符然后是一个单词char,以避免考虑答案。
  • 然后用Q\1 \2替换整个事物:Q问题的第一行是空格,第二行是问题(使用backreferences

您需要多次点击全部替换,直到没有更换出现。

如果有什么不清楚,请告诉我。

<强>试验:

enter image description here

答案 2 :(得分:0)

使用Find + Replace尝试此操作:

(\n\s+)(\s\w)

替换为:

$2