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!
java regex javafx richtextfx
add a comment |
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!
java regex javafx richtextfx
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
add a comment |
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!
java regex javafx richtextfx
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
java regex javafx richtextfx
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 7 at 22:01
Tezra
4,76521039
4,76521039
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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