Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";
// Of course, you need to compile with `Pattern.COMMENTS`
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
if (pattern.matcher(args[0]).matches()) {
 System.out.println("Match!");
} else {
 System.out.println("No match.");
}

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";
// Of course, you need to compile with `Pattern.COMMENTS`
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
if (pattern.matcher(args[0]).matches()) {
 System.out.println("Match!");
} else {
 System.out.println("No match.");
}

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";
// Of course, you need to compile with `Pattern.COMMENTS`
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
if (pattern.matcher(args[0]).matches()) {
 System.out.println("Match!");
} else {
 System.out.println("No match.");
}

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

No #comments in Java!
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";
#// ofOf course, you need to compile with `Pattern.COMMENTS`
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
if (pattern.matcher(args[0]).matches()) {
 System.out.println("Match!");
} else {
 System.out.println("No match.");
}

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";
# of course, you need to compile with `Pattern.COMMENTS`
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
if (pattern.matcher(args[0]).matches()) {
 System.out.println("Match!");
} else {
 System.out.println("No match.");
}

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";
// Of course, you need to compile with `Pattern.COMMENTS`
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
if (pattern.matcher(args[0]).matches()) {
 System.out.println("Match!");
} else {
 System.out.println("No match.");
}

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

add Pattern.COMMENTS
Source Link
Uri Agassi
  • 6.7k
  • 1
  • 18
  • 48

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";
# of course, you need to compile with `Pattern.COMMENTS`
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
if (pattern.matcher(args[0]).matches()) {
 System.out.println("Match!");
} else {
 System.out.println("No match.");
}

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

The regex-native way for commenting is using the /x (free spacing or extended mode) flag.

In many languages there is support for multiline strings, so your regular expression will look like:

regex = /\b # Begin match at the word boundary(whitespace boundary)
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{3} # Match three digits
 [-.]? # Optional - Match dash or dot
 \d{4} # Match four digits
 \b # End match at the word boundary(whitespace boundary)
 /x

In java, however, there is still no way to do this, so you can either comment it as you did, or otherwise carefully construct your string with \n between each line:

String regex = "\\b # Begin match at the word boundary(whitespace boundary)\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{3} # Match three digits\n" +
 "[-.]? # Optional - Match dash or dot\n" +
 "\\d{4} # Match four digits\n" +
 "\\b # End match at the word boundary(whitespace boundary)";
# of course, you need to compile with `Pattern.COMMENTS`
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
if (pattern.matcher(args[0]).matches()) {
 System.out.println("Match!");
} else {
 System.out.println("No match.");
}

The above has no advantage to your suggestion on how to comment your regex, so I guess it a matter of taste...

Source Link
Uri Agassi
  • 6.7k
  • 1
  • 18
  • 48
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /