Multiline regex

I'm new to regex, I have lines like

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" ; I tried

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

but it works not only for one line String

+4
source share
1 answer

Warning: Pattern.MULTILINE does not do what you think. If you want to combine content within input that spans more than one line, you want Pattern.DOTALL : this means that dot and padded character classes should also match newline characters that are not executed by default.

What Pattern.MULTILINE does changes the behavior of the ^ and $ bindings, so that they match after and before the new line, respectively, in addition to matching the start and end of the input (which is their default behavior).

That is, given the input:

 Hello\nworld\n 

Do you have that:

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

Yes, the name MULTILINE confusing. The /m modifier for Perl-like regex engines vs /s ...

+6
source

Source: https://habr.com/ru/post/1490025/


All Articles