4

我是使用正则表达式的新手,我有类似的字符串

ELEMENTS'"MCMCU","MCSTYL","MCDC","MCLDM","MCCO","MCAN8","MCAN8O","MCCNTY","MCADDS","MCFMOD","MCDL01","MCDL02","MCDL03","MCDL04","MCRP01","MCRP02","MCRP03","MCRP04","MCRP05","MCRP06","MCRP07","MCRP08","MCRP09","MCRP10","MCRP11","MCRP12","MCRP13","MCRP14\
","MCRP15","MCRP16","MCRP17","MCRP18","MCRP19","MCRP20","MCRP21","MCRP22","MCRP23","MCRP24","MCRP25","MCRP26","MCRP27","MCRP28","MCRP29","MCRP30","MCTA","MCTXJS","MCTXA1","MCEXR1","MCTC01","MCTC02","MCTC03","MCTC04","MCTC05","MCTC06","MCTC07","MCTC08","\
MCTC09","MCTC10","MCND01","MCND02","MCND03","MCND04","MCND05","MCND06","MCND07","MCND08","MCND09","MCND10","MCCC01","MCCC02","MCCC03","MCCC04","MCCC05","MCCC06","MCCC07","MCCC08","MCCC09","MCCC10","MCPECC","MCALS","MCISS","MCGLBA","MCALCL","MCLMTH","MCL\
F","MCOBJ1","MCOBJ2","MCOBJ3","MCSUB1","MCTOU","MCSBLI","MCANPA","MCCT","MCCERT","MCMCUS","MCBTYP","MCPC","MCPCA","MCPCC","MCINTA","MCINTL","MCD1J","MCD2J","MCD3J","MCD4J","MCD5J","MCD6J","MCFPDJ","MCCAC","MCPAC","MCEEO","MCERC","MCUSER","MCPID","MCUPMJ\
","MCJOBN","MCUPMT","MCBPTP","MCAPSB","MCTSBU"'

我想提取"text1", text2,.....,"textn";我试过了

Pattern p = Pattern.compile("^ELEMENTS\\s'\".*\"'$",Pattern.MULTILINE);
Matcher m = p.matcher(s);

但它不仅适用于一行字符串

4

1 回答 1

6

警告:Pattern.MULTILINE不做你认为它做的事。如果要匹配跨越多行的输入中的内容,则需要Pattern.DOTALL: 这表明点和补充字符类也应该匹配换行符,默认情况下它们不匹配。

所做的是Pattern.MULTILINE改变^$锚的行为,以便它们分别在换行符之后和之前匹配,除了匹配输入的开头和结尾(这是它们的默认行为)。

即,给定输入:

Hello\nworld\n

你有这个:

 Hello \n world \n
|                    # `^` without Pattern.MULTILINE
                  |  # `$` without Pattern.MULTILINE
|        |        |  # `^` with Pattern.MULTILINE
      |        |  |  # `$` with Pattern.MULTILINE

是的,名字MULTILINE很混乱。类似 perl 的正则表达式引擎的/m修饰符与/s...

于 2013-07-06T10:00:35.270 回答