-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?

user3842385
  • 45
  • 10

2 Answers2

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
    
John Kugelman
  • 307,513
  • 65
  • 473
  • 519
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.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • 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. – Stephen C Jul 22 '18 at 02:23