Stephen Corcoran
Stephen Corcoran

Reputation: 346

Javascript to Java Regex

I was wondering if anyone could help me with this regex. When using a regex test and such I get the result that I want however, I am unable to produce the same thing in Java

/^(?: {4})+(?=.+)/gm

Upvotes: 0

Views: 241

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98921

This should do it:

Pattern regex = Pattern.compile("^(?: {4})+(?=.+)", Pattern.MULTILINE);

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Use Pattern.MULTILINE for the /m modifier(or flag). And use the iterator for the /g. And of course remove the delimiters / from the both end of your regex. Please see this example:

String input = "...some input...";
Pattern pattern = Pattern.compile("^(?: {4})+(?=.+)", Pattern.MULTILINE);
Matcher m = pattern.matcher(input);

// Using iterator doing the /g part here
while (m.find()) {
    System.out.println(m.group(1));
}

Upvotes: 1

Shreyos Adikari
Shreyos Adikari

Reputation: 12744

Remove /gm form the regex. This is javascript regex not java.
If you want to convert javascript regex to java, we already have an answer here. See How to convert javascript regex to safe java regex?

Upvotes: 1

Related Questions