RichTextFX comment and property regex











up vote
0
down vote

favorite












I am trying to use the RichTextFX library to show a properties file in the CodeArea. I have created some Regex for comments as well as properties to try to style to CodeArea.
EDIT: I am able to get them working properly. Except for when I add a comment to a line that has a property. My regex for determining a Property is not able to tell when there's a # at the beginning of the line. I have tried but am unable to get it to negate the rest of it if it sees there is a # at the beginning.



Here's the two patterns that I am using:



Pattern COMMENT = Pattern.compile("(?<SingleLineComment>#+)(?<Comment>.*n)");



Pattern PROPERTY = Pattern.compile("(n?(?<PropertyName>.+)(\h*=\h*)(?<PropertyValue>\S+))");



Here's how they are being used in code:



import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest extends Application
{
public static void main (String args) {
launch(args);
}



@Override
public void start (Stage primaryStage) throws Exception {
_area = new CodeArea();
_area.getStylesheets().add(getClass().getResource("/css/settings-design.css").toExternalForm());
_area.setParagraphGraphicFactory(LineNumberFactory.get(_area));

_area.textProperty().addListener((obs, oldValue, newValue) -> {
computeHighlighting(newValue);
});

primaryStage.setScene(new Scene(_area, 400, 600));

primaryStage.show();

_area.appendText(PROPERTIES);
}

private void computeHighlighting(String text) {
Matcher equalsTest = COMMENT.matcher(text);
while (equalsTest.find()){
_area.setStyle( equalsTest.start(), equalsTest.end(), Collections.singleton("comment"));
}

Matcher propertyMatcher = PROPERTY.matcher(text);
while (propertyMatcher.find()){
_area.setStyle(propertyMatcher.start(1), propertyMatcher.end(3), Collections.singleton("property-name"));
_area.setStyle(propertyMatcher.start(4), propertyMatcher.end(4), Collections.singleton("property-value"));
}
}

private CodeArea _area;

private static final Pattern COMMENT = Pattern.compile("(?<SingleLineComment>#+\h*)(?<Comment>.*)n?");
private static final Pattern PROPERTY = Pattern.compile("(?:[#].+|(n?(?<PropertyName>.+)(\h*=\h*)(?<PropertyValue>\S+)))");

private static final Pattern EQUALSTEST = Pattern.compile("=");

public static final String PROPERTIES = "#Tue Nov 06 12:42:33 CST 2018n" +
"#prop0=true";


And code-area.css:



.comment {
-fx-fill: #008e00;
}

.property-name {
-fx-fill: #ff8e31;
}

.property-value {
-fx-fill: #58a3ff;
}


Thank you in advance for your help!










share|improve this question
























  • Could you please include the error you're seeing?
    – Tim
    Nov 7 at 19:31










  • The error isn't the problem. I know that the error that's being thrown is because the Regex is not matching properly
    – Blake Ordway
    Nov 7 at 20:04










  • OK, well we know that now, which is a step forwards. If that's not the problem, could you describe what you expect to see happen, and what does happen instead.
    – Tim
    Nov 7 at 20:14










  • Tim, I updated my post since I figured out most of the problem. The new problem now is in the edit part of the post.
    – Blake Ordway
    Nov 7 at 21:21















up vote
0
down vote

favorite












I am trying to use the RichTextFX library to show a properties file in the CodeArea. I have created some Regex for comments as well as properties to try to style to CodeArea.
EDIT: I am able to get them working properly. Except for when I add a comment to a line that has a property. My regex for determining a Property is not able to tell when there's a # at the beginning of the line. I have tried but am unable to get it to negate the rest of it if it sees there is a # at the beginning.



Here's the two patterns that I am using:



Pattern COMMENT = Pattern.compile("(?<SingleLineComment>#+)(?<Comment>.*n)");



Pattern PROPERTY = Pattern.compile("(n?(?<PropertyName>.+)(\h*=\h*)(?<PropertyValue>\S+))");



Here's how they are being used in code:



import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest extends Application
{
public static void main (String args) {
launch(args);
}



@Override
public void start (Stage primaryStage) throws Exception {
_area = new CodeArea();
_area.getStylesheets().add(getClass().getResource("/css/settings-design.css").toExternalForm());
_area.setParagraphGraphicFactory(LineNumberFactory.get(_area));

_area.textProperty().addListener((obs, oldValue, newValue) -> {
computeHighlighting(newValue);
});

primaryStage.setScene(new Scene(_area, 400, 600));

primaryStage.show();

_area.appendText(PROPERTIES);
}

private void computeHighlighting(String text) {
Matcher equalsTest = COMMENT.matcher(text);
while (equalsTest.find()){
_area.setStyle( equalsTest.start(), equalsTest.end(), Collections.singleton("comment"));
}

Matcher propertyMatcher = PROPERTY.matcher(text);
while (propertyMatcher.find()){
_area.setStyle(propertyMatcher.start(1), propertyMatcher.end(3), Collections.singleton("property-name"));
_area.setStyle(propertyMatcher.start(4), propertyMatcher.end(4), Collections.singleton("property-value"));
}
}

private CodeArea _area;

private static final Pattern COMMENT = Pattern.compile("(?<SingleLineComment>#+\h*)(?<Comment>.*)n?");
private static final Pattern PROPERTY = Pattern.compile("(?:[#].+|(n?(?<PropertyName>.+)(\h*=\h*)(?<PropertyValue>\S+)))");

private static final Pattern EQUALSTEST = Pattern.compile("=");

public static final String PROPERTIES = "#Tue Nov 06 12:42:33 CST 2018n" +
"#prop0=true";


And code-area.css:



.comment {
-fx-fill: #008e00;
}

.property-name {
-fx-fill: #ff8e31;
}

.property-value {
-fx-fill: #58a3ff;
}


Thank you in advance for your help!










share|improve this question
























  • Could you please include the error you're seeing?
    – Tim
    Nov 7 at 19:31










  • The error isn't the problem. I know that the error that's being thrown is because the Regex is not matching properly
    – Blake Ordway
    Nov 7 at 20:04










  • OK, well we know that now, which is a step forwards. If that's not the problem, could you describe what you expect to see happen, and what does happen instead.
    – Tim
    Nov 7 at 20:14










  • Tim, I updated my post since I figured out most of the problem. The new problem now is in the edit part of the post.
    – Blake Ordway
    Nov 7 at 21:21













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to use the RichTextFX library to show a properties file in the CodeArea. I have created some Regex for comments as well as properties to try to style to CodeArea.
EDIT: I am able to get them working properly. Except for when I add a comment to a line that has a property. My regex for determining a Property is not able to tell when there's a # at the beginning of the line. I have tried but am unable to get it to negate the rest of it if it sees there is a # at the beginning.



Here's the two patterns that I am using:



Pattern COMMENT = Pattern.compile("(?<SingleLineComment>#+)(?<Comment>.*n)");



Pattern PROPERTY = Pattern.compile("(n?(?<PropertyName>.+)(\h*=\h*)(?<PropertyValue>\S+))");



Here's how they are being used in code:



import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest extends Application
{
public static void main (String args) {
launch(args);
}



@Override
public void start (Stage primaryStage) throws Exception {
_area = new CodeArea();
_area.getStylesheets().add(getClass().getResource("/css/settings-design.css").toExternalForm());
_area.setParagraphGraphicFactory(LineNumberFactory.get(_area));

_area.textProperty().addListener((obs, oldValue, newValue) -> {
computeHighlighting(newValue);
});

primaryStage.setScene(new Scene(_area, 400, 600));

primaryStage.show();

_area.appendText(PROPERTIES);
}

private void computeHighlighting(String text) {
Matcher equalsTest = COMMENT.matcher(text);
while (equalsTest.find()){
_area.setStyle( equalsTest.start(), equalsTest.end(), Collections.singleton("comment"));
}

Matcher propertyMatcher = PROPERTY.matcher(text);
while (propertyMatcher.find()){
_area.setStyle(propertyMatcher.start(1), propertyMatcher.end(3), Collections.singleton("property-name"));
_area.setStyle(propertyMatcher.start(4), propertyMatcher.end(4), Collections.singleton("property-value"));
}
}

private CodeArea _area;

private static final Pattern COMMENT = Pattern.compile("(?<SingleLineComment>#+\h*)(?<Comment>.*)n?");
private static final Pattern PROPERTY = Pattern.compile("(?:[#].+|(n?(?<PropertyName>.+)(\h*=\h*)(?<PropertyValue>\S+)))");

private static final Pattern EQUALSTEST = Pattern.compile("=");

public static final String PROPERTIES = "#Tue Nov 06 12:42:33 CST 2018n" +
"#prop0=true";


And code-area.css:



.comment {
-fx-fill: #008e00;
}

.property-name {
-fx-fill: #ff8e31;
}

.property-value {
-fx-fill: #58a3ff;
}


Thank you in advance for your help!










share|improve this question















I am trying to use the RichTextFX library to show a properties file in the CodeArea. I have created some Regex for comments as well as properties to try to style to CodeArea.
EDIT: I am able to get them working properly. Except for when I add a comment to a line that has a property. My regex for determining a Property is not able to tell when there's a # at the beginning of the line. I have tried but am unable to get it to negate the rest of it if it sees there is a # at the beginning.



Here's the two patterns that I am using:



Pattern COMMENT = Pattern.compile("(?<SingleLineComment>#+)(?<Comment>.*n)");



Pattern PROPERTY = Pattern.compile("(n?(?<PropertyName>.+)(\h*=\h*)(?<PropertyValue>\S+))");



Here's how they are being used in code:



import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest extends Application
{
public static void main (String args) {
launch(args);
}



@Override
public void start (Stage primaryStage) throws Exception {
_area = new CodeArea();
_area.getStylesheets().add(getClass().getResource("/css/settings-design.css").toExternalForm());
_area.setParagraphGraphicFactory(LineNumberFactory.get(_area));

_area.textProperty().addListener((obs, oldValue, newValue) -> {
computeHighlighting(newValue);
});

primaryStage.setScene(new Scene(_area, 400, 600));

primaryStage.show();

_area.appendText(PROPERTIES);
}

private void computeHighlighting(String text) {
Matcher equalsTest = COMMENT.matcher(text);
while (equalsTest.find()){
_area.setStyle( equalsTest.start(), equalsTest.end(), Collections.singleton("comment"));
}

Matcher propertyMatcher = PROPERTY.matcher(text);
while (propertyMatcher.find()){
_area.setStyle(propertyMatcher.start(1), propertyMatcher.end(3), Collections.singleton("property-name"));
_area.setStyle(propertyMatcher.start(4), propertyMatcher.end(4), Collections.singleton("property-value"));
}
}

private CodeArea _area;

private static final Pattern COMMENT = Pattern.compile("(?<SingleLineComment>#+\h*)(?<Comment>.*)n?");
private static final Pattern PROPERTY = Pattern.compile("(?:[#].+|(n?(?<PropertyName>.+)(\h*=\h*)(?<PropertyValue>\S+)))");

private static final Pattern EQUALSTEST = Pattern.compile("=");

public static final String PROPERTIES = "#Tue Nov 06 12:42:33 CST 2018n" +
"#prop0=true";


And code-area.css:



.comment {
-fx-fill: #008e00;
}

.property-name {
-fx-fill: #ff8e31;
}

.property-value {
-fx-fill: #58a3ff;
}


Thank you in advance for your help!







java regex javafx richtextfx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 21:20

























asked Nov 7 at 19:16









Blake Ordway

1837




1837












  • Could you please include the error you're seeing?
    – Tim
    Nov 7 at 19:31










  • The error isn't the problem. I know that the error that's being thrown is because the Regex is not matching properly
    – Blake Ordway
    Nov 7 at 20:04










  • OK, well we know that now, which is a step forwards. If that's not the problem, could you describe what you expect to see happen, and what does happen instead.
    – Tim
    Nov 7 at 20:14










  • Tim, I updated my post since I figured out most of the problem. The new problem now is in the edit part of the post.
    – Blake Ordway
    Nov 7 at 21:21


















  • Could you please include the error you're seeing?
    – Tim
    Nov 7 at 19:31










  • The error isn't the problem. I know that the error that's being thrown is because the Regex is not matching properly
    – Blake Ordway
    Nov 7 at 20:04










  • OK, well we know that now, which is a step forwards. If that's not the problem, could you describe what you expect to see happen, and what does happen instead.
    – Tim
    Nov 7 at 20:14










  • Tim, I updated my post since I figured out most of the problem. The new problem now is in the edit part of the post.
    – Blake Ordway
    Nov 7 at 21:21
















Could you please include the error you're seeing?
– Tim
Nov 7 at 19:31




Could you please include the error you're seeing?
– Tim
Nov 7 at 19:31












The error isn't the problem. I know that the error that's being thrown is because the Regex is not matching properly
– Blake Ordway
Nov 7 at 20:04




The error isn't the problem. I know that the error that's being thrown is because the Regex is not matching properly
– Blake Ordway
Nov 7 at 20:04












OK, well we know that now, which is a step forwards. If that's not the problem, could you describe what you expect to see happen, and what does happen instead.
– Tim
Nov 7 at 20:14




OK, well we know that now, which is a step forwards. If that's not the problem, could you describe what you expect to see happen, and what does happen instead.
– Tim
Nov 7 at 20:14












Tim, I updated my post since I figured out most of the problem. The new problem now is in the edit part of the post.
– Blake Ordway
Nov 7 at 21:21




Tim, I updated my post since I figured out most of the problem. The new problem now is in the edit part of the post.
– Blake Ordway
Nov 7 at 21:21












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Here is a regex for parsing a property file line into it's key-divider-value-comment parts that is Java complaint.



^s*(?:|(?<PropertyName>w+(?:.w+)*)(?<Divider>s*[=: ]s*)(?<PropertyValue>w(?:.|\,|\[nrfv]+)*))(?<Comment>[#!].*)?$


Note that this regex is still not exhaustive, and the built in Properties class is much more suited to this task than regex. (Use the load function to parse the source, and than it basically works like a Map)






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196296%2frichtextfx-comment-and-property-regex%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Here is a regex for parsing a property file line into it's key-divider-value-comment parts that is Java complaint.



    ^s*(?:|(?<PropertyName>w+(?:.w+)*)(?<Divider>s*[=: ]s*)(?<PropertyValue>w(?:.|\,|\[nrfv]+)*))(?<Comment>[#!].*)?$


    Note that this regex is still not exhaustive, and the built in Properties class is much more suited to this task than regex. (Use the load function to parse the source, and than it basically works like a Map)






    share|improve this answer

























      up vote
      0
      down vote













      Here is a regex for parsing a property file line into it's key-divider-value-comment parts that is Java complaint.



      ^s*(?:|(?<PropertyName>w+(?:.w+)*)(?<Divider>s*[=: ]s*)(?<PropertyValue>w(?:.|\,|\[nrfv]+)*))(?<Comment>[#!].*)?$


      Note that this regex is still not exhaustive, and the built in Properties class is much more suited to this task than regex. (Use the load function to parse the source, and than it basically works like a Map)






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Here is a regex for parsing a property file line into it's key-divider-value-comment parts that is Java complaint.



        ^s*(?:|(?<PropertyName>w+(?:.w+)*)(?<Divider>s*[=: ]s*)(?<PropertyValue>w(?:.|\,|\[nrfv]+)*))(?<Comment>[#!].*)?$


        Note that this regex is still not exhaustive, and the built in Properties class is much more suited to this task than regex. (Use the load function to parse the source, and than it basically works like a Map)






        share|improve this answer












        Here is a regex for parsing a property file line into it's key-divider-value-comment parts that is Java complaint.



        ^s*(?:|(?<PropertyName>w+(?:.w+)*)(?<Divider>s*[=: ]s*)(?<PropertyValue>w(?:.|\,|\[nrfv]+)*))(?<Comment>[#!].*)?$


        Note that this regex is still not exhaustive, and the built in Properties class is much more suited to this task than regex. (Use the load function to parse the source, and than it basically works like a Map)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 22:01









        Tezra

        4,76521039




        4,76521039






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196296%2frichtextfx-comment-and-property-regex%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()