2

I am reading though some old code that seems to aim to take out a sequence of periods (......) in a table of contents. It utilizes a java regular expression to accomplish this. This is the code that was used.

input = input.replaceAll( ".*<elipses>.*", "" );

However I don't see mention of a regex with "<>'s" except here:

Special constructs (named-capturing and non-capturing)
(?<name>X) X, as a named-capturing group

(http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html)

What does the <elipsis> mean?

4
  • Are you sure you don't have literally <elipses> in your text? .* is greedy so I don't see how <elipses> would work as a match for .... Commented Mar 21, 2014 at 16:58
  • Is that actually the literal code copied and pasted, as opposed to something out of documentation? Commented Mar 21, 2014 at 16:58
  • The above code is what was in the old code. I was just trying to understand what they were originally trying to do. It is not out of Java's documentation. Commented Mar 21, 2014 at 16:59
  • regex101.com/r/bW8tL3 Commented Mar 21, 2014 at 17:02

1 Answer 1

3

That looks like a Java 7 named group with what appears to be incorrect syntax.

The correct syntax would be something like:

//                           | named group "stuff" matches 0 or more characters
//                           |           | named group "ellipses" 3 consecutive dots
//                           |           |                     | non grouped stuff
Pattern p = Pattern.compile("(?<stuff>.*)(?<ellipses>\\.\\.\\.).*");
String input = "blah ... blah";
Matcher m = p.matcher(input);
if (m.find()) {
    // printing back reference to named group "stuff"
    System.out.println(m.group("stuff"));
    // printing back reference to named group "ellipses"
    System.out.println(m.group("ellipses"));
}

Output

blah 
...

Otherwise your Pattern is attempting to actually match some markup containing <ellipses>, preceded by any number of characters, followed by the same.

Some API reference to Java 7 named groups here.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool that you can do this.

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.