rspec/rules/S2639/java/rule.adoc
Alban Auzeill 2c306d110e Fix code block ambiguity with old header style
Ensure blank line before list and clean the one leading space
2020-06-30 17:16:12 +02:00

22 lines
1.1 KiB
Plaintext

Regular expressions are powerful but tricky, and even those long used to using them can make mistakes.
The following should not be used as regular expressions:
* <code>.</code> - matches any single character. Used in <code>replaceAll</code>, it matches _everything_
* <code>|</code> - normally used as an option delimiter. Used stand-alone, it matches the space between characters
* <code>File.separator</code> - matches the platform-specific file path delimiter. On Windows, this will be taken as an escape character
== Noncompliant Code Example
----
String str = "/File|Name.txt";
String clean = str.replaceAll(".",""); // Noncompliant; probably meant to remove only dot chars, but returns an empty string
String clean2 = str.replaceAll("|","_"); // Noncompliant; yields _/_F_i_l_e_|_N_a_m_e_._t_x_t_
String clean3 = str.replaceAll(File.separator,""); // Noncompliant; exception on Windows
String clean4 = str.replaceFirst(".",""); // Noncompliant;
String clean5 = str.replaceFirst("|","_"); // Noncompliant;
String clean6 = str.replaceFirst(File.separator,""); // Noncompliant;
----