Results 1 to 2 of 2

Thread: Multiline Regex

  1. #1
    zriggle is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default 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:
    Java Code:
    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".

    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:
    Java Code:
    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:
    		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(">> " +;
    Any assistance would be greatly appreciated :-D

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Rep Power


    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.

Similar Threads

  1. To get multiline tooltip
    By diva_garg in forum New To Java
    Replies: 8
    Last Post: 03-25-2011, 04:16 PM
  2. How to write multiline String in a JLabel
    By JavaBean in forum AWT / Swing
    Replies: 4
    Last Post: 12-14-2009, 05:09 AM
  3. Some help with regex and loop
    By moaxjlou in forum New To Java
    Replies: 21
    Last Post: 11-02-2008, 10:24 PM
  4. [SOLVED] More RegEx help
    By JT4NK3D in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 04:07 AM
  5. Regex pattern
    By ravian in forum New To Java
    Replies: 4
    Last Post: 12-11-2007, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts