简体   繁体   English

多行正则表达式

[英]Multiline regular expression

I'm new using regex , i have strings like 我是使用regex的新手,我有类似的字符串

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"'

i want to extract "text1", text2,.....,"textn" ; 我想提取"text1", text2,.....,"textn" i tried 我试过了

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

but it doesn't works only for one line String 但它仅对一行字符串无效

Warning: Pattern.MULTILINE does not do what you think it does. 警告: Pattern.MULTILINE不会执行您认为的操作。 If you want to match content within an input which spans more than one line, you want Pattern.DOTALL : this tells that the dot and complemented character classes should also match newlines, which they do not by default. 如果要匹配跨越多行的输入中的内容,则需要Pattern.DOTALL :这表明点和补码字符类也应匹配换行符,默认情况下不匹配换行符。

What Pattern.MULTILINE does is changing the behaviour of the ^ and $ anchors, so that they match after and before a newline respectively, in addition to matching the beginning and end of input (which is their default behaviour). Pattern.MULTILINE所做的是改变^$锚的行为,以便除了匹配输入的开头和结尾(这是它们的默认行为)之外,它们还分别在换行符之后和之前匹配。

Ie, given the input: 即,鉴于输入:

Hello\nworld\n

you have this: 你有这个:

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

Yes, the name MULTILINE is confusing. 是的,名称MULTILINE令人困惑。 So is the /m modifier of perl-like regex engines vs /s ... 像perl一样的正则表达式引擎的/m修饰符vs /s ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM