0

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
3
  • We're gonna need a little bit more than that... What's your input? What are the two ouputs? What do you want to do? Please edit the question to answer and give some examples. Commented Apr 15, 2014 at 21:05
  • Apart from all those this regex looks suspicious too Commented Apr 15, 2014 at 21:06
  • get rid of / and /gm Commented Apr 15, 2014 at 21:07

3 Answers 3

1

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));
}
Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

0

This should do it:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.