正则表达式,用于匹配特定的字符

时间:2019-06-06 16:45:42

标签: arrays regex

将问题文本与问题选项分开。我怎么得到这个?

我尝试过,但是效果不佳:

 /(\d+)\.\s*([A-Z].*?)\s+([A-Z]\..*?)\s*[A-Z]|$)/s

纯文字:

  
      
  1. Whatti ..
  2.   

目标:

  

什么?

     

A。茂盛的身影

     

B)皮尔

     

C; CLA附件

     

D:上颌可移动的部分层状假牙

     

E:Swa部分

     
      
  1. 7-腮腺炎(腮腺炎)
  2.   

这是一个问题文本。问题文本中的选项指定为(A,B,C,D,E ..)。我正在尝试区分这些问题选项。

2 个答案:

答案 0 :(得分:0)

如果我们只希望检测列出的三个字符,我们将从一个简单的表达式开始,例如:

(\.|\(|\))

Demo 1

为了达到预期的目标,我们的表达式将变得更加复杂,我们可以从以下表达式开始:

([A-Z]\.|[A-Z]\)|[A-Z]:|[A-Z];|[0-9]+\.)

,我们将添加到其中,因为使用交替可能会出现新的情况。

Demo 2

测试

$re = '/([A-Z]\.|[A-Z]\)|[A-Z]:|[A-Z];|[0-9]+\.)/m';
$str = 'What denture construction would be optimal for this patient, considering his occupation as a lecturer? A.Porcelain-fused-to-metal dental bridge B)Plastic dental bridge C;Clasp-retained (bugel) removable partial denture with attachments D:Removable partial laminar denture for the upper jaw E.Swaged-soldered metal dental bridge with faceted intermediate part 13. A 7-year-old boy is diagnosed with epi- demic parotitis (mumps).';
$subst = '\\n Option: $1 ';

$result = preg_replace($re, $subst, $str);

echo $result;

RegEx电路

jex.im可视化正则表达式:

enter image description here

答案 1 :(得分:0)

这不漂亮,但是可以做到:

$text = "What denture construction would be optimal for this patient, considering his occupation as a lecturer? A.Porcelain-fused-to-metal dental bridge B)Plastic dental bridge C;Clasp-retained (bugel) removable partial denture with attachments D:Removable partial laminar denture for the upper jaw E.Swaged-soldered metal dental bridge with faceted intermediate part 13. A 7-year-old boy is diagnosed with epi- demic parotitis (mumps).";

$symbol = ":";
//Put whatever symbol you wish to use to create a more uniform output

$replacement = preg_replace("/((?:[A-Z]{1}|[0-9]{2}))((?:\\.|\\)|;|:))([ A-Z]{1})/", "\r\n$1{$symbol} $3", $text);

echo $replacement;

这将输出:

What denture construction would be optimal for this patient, considering his occupation as a lecturer? 
A: Porcelain-fused-to-metal dental bridge 
B: Plastic dental bridge 
C: Clasp-retained (bugel) removable partial denture with attachments 
D: Removable partial laminar denture for the upper jaw 
E: Swaged-soldered metal dental bridge with faceted intermediate part 
13:  A 7-year-old boy is diagnosed with epi- demic parotitis (mumps).

注意,因为正则表达式非常灵活,所以误报的可能性非常高。

最好清理输入。如果我猜到了,我会猜到您输入的是PDF复制/粘贴作业。查看是否可以从源导入更多格式。

演示:https://3v4l.org/h4OA9