Results 1 to 2 of 2
Thread: Multiline Regex
- 02-26-2009, 12:39 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
Multiline Regex
Hi All
I'm having a bit of trouble figuring out how one implements regex matching of content in a file with Java (I like to play around with different languages, so this is my little foray into Java).
What I have is a file that looks like below:
And I am trying to create a pattern-matcher with regular expressions that will parse the file, and allow me to access the information contained in the file easily (for example, giving me an associative array so that I could access data[0].get("A.A1") and have it retrieve "SomeStuff".Java Code:Start={ A={ A1={ SomeStuff } A2={SomeStuff} } B={SomeOtherStuff} C={ YetMoreStuff.Fun.FQDN } } Start={ Alpha={ OtherStuff } }
However, I'm having a lot of hassle even getting the pattern-matching to work in the first place. It seems like something that would be easiest by peeling away at the layers (i.e. getting everything inside of Start={...}, then getting at everything inside of A={...}, and so on), but I can't even figure out how I'm supposed to match the content inside of Start={...}.
What I've come up with (granted, it does not work) is:
Which should match the beginning "Out={" statement, any number of lines that do not begin with "}", and stop when it encounters the first line that begins with "}". I've tried to make this simpler by ignoring the issue of using the Scanner object for file manipulation, and just using strings like so:Java Code:Out=\\{^[^\\}]*^\\}+?
Any assistance would be greatly appreciated :-DJava Code:String expression = "Out=\\{^[^\\}]*^\\}*?"; String searchme = "Out={\n One\n}\nOut={\n Two\n}"; Pattern p = Pattern.compile(expression, Pattern.MULTILINE | Pattern.DOTALL); Matcher m = p.matcher(searchme); while( m.find() ) { System.out.println(">> " + m.group(0)); }
- 02-26-2009, 04:49 AM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
In your regex, I don't quite see what the first ^ character is for. If you want to use it to exclude certain characters, it always goes inside square brackets, as indeed you have in the next part of your expression.
Note that usually, MULTILINE is used to mean "I care about line breaks, and am going to use the ^ and $ symbols in my regex to specifically match against start and end of line". In this mode, when you match against "anything", you actually match against "anything minus line break characters", which may not be the behaviour you want here.
In case it's helpful, I've also written a Java regular expressions tutorial which mentions this and various other issues.Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
To get multiline tooltip
By diva_garg in forum New To JavaReplies: 8Last Post: 03-25-2011, 04:16 PM -
How to write multiline String in a JLabel
By JavaBean in forum AWT / SwingReplies: 4Last Post: 12-14-2009, 05:09 AM -
Some help with regex and loop
By moaxjlou in forum New To JavaReplies: 21Last Post: 11-02-2008, 10:24 PM -
[SOLVED] More RegEx help
By JT4NK3D in forum New To JavaReplies: 2Last Post: 05-23-2008, 04:07 AM -
Regex pattern
By ravian in forum New To JavaReplies: 4Last Post: 12-11-2007, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks