Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (778)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java regex pattern for finding text between [[ ]]  (Read 1721 times)
0 Members and 1 Guest are viewing this topic.
Offline orange451

JGO Kernel


Medals: 457
Projects: 7
Exp: 7 years


Your face? Your ass? What's the difference?


« Posted 2018-03-03 22:33:33 »

Hiah.

I'm implementing a lua text editor into my engine, and wanted to create syntax highlighting that'll work with lua. Most of it works so far, but I can't wrap my head around multiline strings.

Unlike java, where multiline strings are done with /* and */, lua has them between these characters: [[ and ]], but I'm not much of a regex expert and was hoping someone could help me out with this issue.


First Recon. A java made online first person shooter!
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 287
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #1 - Posted 2018-03-04 00:39:46 »

Maybe I'm missing something, but wouldn't this just be:

1  
[[.*]]


Note that brackets [] are a special character in regex, so you have to escape them:

1  
\[\[.*\]\]


And of course, backslashes are a special character in Java, so you have to escape them:

1  
String regex = "\\[\\[.*\\]\\]";


More info can be found in the Pattern class.

If that's not what you're looking for, can you please post a small example showing the kinds of Strings you're matching and what you're trying to do with the regex?

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline CommanderKeith
« Reply #2 - Posted 2018-03-04 02:11:19 »

Geez, regex appears to be a programming language within itself
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#sum

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline orange451

JGO Kernel


Medals: 457
Projects: 7
Exp: 7 years


Your face? Your ass? What's the difference?


« Reply #3 - Posted 2018-03-04 03:09:05 »

@KevinWorkman
Tried your idea out, but unfortunately there's no effect


For comparison, here's what I found online for finding a string surrounded by " marks:
1  
"\"([^\"\\\\]|\\\\.)*\""

First Recon. A java made online first person shooter!
Online Phased
« Reply #4 - Posted 2018-03-04 04:16:21 »

1  
\[\[(.|\n)*\]\]


with \ escaped:
1  
\\[\\[(.|\\n)*\\]\\]


that should work.

you can try it out here
Offline SHC
« Reply #5 - Posted 2018-03-04 04:57:47 »

Did you add the multiline option to the Pattern?

1  
Pattern regex = Pattern.compile("--\\[\\[.+?\\]\\]", Pattern.MULTILINE);

Otherwise, regex will stop at a single line.

Online Phased
« Reply #6 - Posted 2018-03-04 05:05:25 »

Did you add the multiline option to the Pattern?

1  
Pattern regex = Pattern.compile("--\\[\\[.+?\\]\\]", Pattern.MULTILINE);

Otherwise, regex will stop at a single line.

that won't work.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
import java.util.regex.Pattern;

public class MyClass {
    public static void main(String args[]) {
        String str = "hello[[ abc \n def \n ghi ]] world";
        Pattern regex = Pattern.compile("--\\[\\[.+?\\]\\]", Pattern.MULTILINE);
        String shc = regex.matcher( str ).replaceAll( "" );
        String phased = str.replaceAll("\\[\\[(.|\\n)*\\]\\]", "");
        System.out.println("SHC:" + shc);
        System.out.println("Phased:" + phased);
    }
}


gives the output:
1  
2  
3  
4  
SHC:hello[[ abc 
 def
 ghi ]] world
Phased:hello world
Offline orange451

JGO Kernel


Medals: 457
Projects: 7
Exp: 7 years


Your face? Your ass? What's the difference?


« Reply #7 - Posted 2018-03-04 06:08:54 »

Couldn't seem to get yours to work, phased.

Though from your guys responses, I managed to write this:
1  
"(\\[\\[)(.|\\R)*(\\]\\])"


Which seems to get the job done perfectly... until there's two sets.


The second "]]" causes the entire script to be highlighted, as it regards the second "[[" as part of the inner text from the first "[[".

Hmm...

First Recon. A java made online first person shooter!
Online Phased
« Reply #8 - Posted 2018-03-04 08:03:20 »

The second "]]" causes the entire script to be highlighted, as it regards the second "[[" as part of the inner text from the first "[[".
Hmm...

Here is a fix.

Without testing it, this may be the fix for you:
1  
"(\\[\\[)(.|\\R)*?(\\]\\])"


? will make it non greedy.

it seems \r is your line break in your editor instead of \n, so I guess that is why mine does not work for you.

The other problem will be that it will detect comments within ", so you will also need to work out how to get around that.
Offline Abuse

JGO Ninja


Medals: 70


falling into the abyss of reality


« Reply #9 - Posted 2018-03-04 11:30:20 »

Don't forget to include lots and LOTS of unit tests if you want that regex to be anything but a blackbox when you (or some unlucky other) come back to it in a few months time.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #10 - Posted 2018-03-04 14:20:16 »

? will make it non greedy.

Yes, that'll be important.

it seems \r is your line break in your editor instead of \n

Unlikely, unless we're gone back significantly in time, but \r\n is likely.  I'd missed the addition of \R in Java 8 - nice!

https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#lineending

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline orange451

JGO Kernel


Medals: 457
Projects: 7
Exp: 7 years


Your face? Your ass? What's the difference?


« Reply #11 - Posted 2018-03-04 16:10:04 »

it seems \r is your line break in your editor instead of \n, so I guess that is why mine does not work for you.
\R in javas regex is any combination of linebreak characters... I think...?

The other problem will be that it will detect comments within ", so you will also need to work out how to get around that.
It all matters which one I check first. If I check for multiline comments before I check for quotation comments then the multiline will be captured. but even IF it doesn't work, they're the same color... so the user will never know! Smiley

Your fixed version works perfectly, though!

First Recon. A java made online first person shooter!
Offline Longarmx
« Reply #12 - Posted 2018-03-05 04:12:39 »

Regex was a nightmare before I learned it, but now, it's a lucid nightmare.

But in all seriousness, regex is a very useful skill and I would recommend anyone take the time to learn it.

Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (350 views)
2018-11-16 13:46:03

hadezbladez (183 views)
2018-11-16 13:41:33

hadezbladez (349 views)
2018-11-16 13:35:35

hadezbladez (85 views)
2018-11-16 13:32:03

EgonOlsen (2179 views)
2018-06-10 19:43:48

EgonOlsen (2221 views)
2018-06-10 19:43:44

EgonOlsen (1379 views)
2018-06-10 19:43:20

DesertCoockie (2012 views)
2018-05-13 18:23:11

nelsongames (1671 views)
2018-04-24 18:15:36

nelsongames (2309 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!