-3

The pattern is from the following Snippet.

private static Pattern specialDoubleRegex = Pattern.compile("((-)?infinityd?)|(nand?)", Pattern.CASE_INSENSITIVE);

I am a beginner. I am starting to use Pattern class to write my own regex. I saw this example in the Java code example.I don’t completely understand them. Are we using special construct (?<name>X)? According to precious post and javase 7 docs, ?infinityd?) is not special construct here. what is (-)?infinityd? about? Can I say that they are the pattern looks like ((X)?XY?)|(X?) pattern?

0

2 Answers 2

1

No, these are regular question marks that make the previous character or group optional.

  • (-)?infinityd? matches "infinity" with an optional minus sign in front and an optional trailing "d". If the minus sign is present it is captured in group 2.

    infinity
    infinityd
    -infinity
    -infinityd
    
  • nand? matches "nan" with an optional trailing "d".

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

Comments

0

Are we using special construct (?<name>X)?

To be clear, the "Special constructs (named capturing and non-capturing)" described in the javadoc for Pattern consist of a ( immediately followed by a ?.

In your example, the ? symbols follow some other character. As John explains, they are simple (greedy) quantifiers in each case.

1 Comment

Yes, I know he is right. And so am I. There is no named capturing group here because ... as I explained ... this is not the correct syntax for a "special construct". Refer to the javadocs to understand the syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.