Reputation: 99
I'm new using regex , i have strings 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 doesn't works only for one line String
Upvotes: 4
Views: 230
Reputation: 121702
Warning: Pattern.MULTILINE
does not do what you think it does. 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.
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).
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. So is the /m
modifier of perl-like regex engines vs /s
...
Upvotes: 6