Java如何使用正则表达式匹配 “--” 和 “/**/”两种类型的注释语句
时间: 2023-12-04 09:05:55 浏览: 64
可以使用 Java 中的正则表达式和 String 类的 replaceAll() 方法来匹配 MySQL 中的两种注释语句。
以下是一个示例代码:
```java
String sql = "SELECT * FROM users WHERE id = 1 -- AND name = 'John' ";
String regex = "--.*|/\\*.*?\\*/"; // 匹配 -- 和 /* */ 注释
String result = sql.replaceAll(regex, ""); // 删除注释语句
System.out.println(result); // 输出:SELECT * FROM users WHERE id = 1
```
在上面的代码中,首先定义了一个 MySQL 查询语句,并使用正则表达式匹配注释语句。正则表达式 `--.*|/\\*.*?\\*/` 可以匹配 `--` 和 `/* */` 注释,其中 `--.*` 表示匹配以 `--` 开头的注释,`/\\*.*?\\*/` 表示匹配 `/* */` 中间的注释内容,其中 `.*?` 表示非贪婪匹配,以避免匹配到多个注释语句。
然后使用 String 类的 replaceAll() 方法将匹配到的注释语句删除,最后输出结果为不包含注释语句的查询语句。
相关问题
Java如何使用正则表达式匹配 “--” 和 “/**/”两种类型的注释语句,要考虑到注释内容换行,注释符号为内容的情况
可以使用Java中的正则表达式来匹配代码中的注释语句,以下是示例代码:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommentMatcher {
public static void main(String[] args) {
String code = "/* This is a\n" +
"multiline\n" +
"comment */\n" +
"int x = 5; // This is a single-line comment\n" +
"int y = 10; -- This is another single-line comment\n" +
"String str = \"/* This is not a comment */\"; // This is a comment\n" +
"/* This is a comment with -- in it */";
// 匹配 /* */ 形式的注释
Pattern multilineCommentPattern = Pattern.compile("/\\*([\\s\\S]*?)\\*/");
Matcher multilineCommentMatcher = multilineCommentPattern.matcher(code);
while (multilineCommentMatcher.find()) {
System.out.println("Multiline comment: " + multilineCommentMatcher.group(1));
}
// 匹配 // 形式的注释
Pattern singleLineCommentPattern = Pattern.compile("//(.*)");
Matcher singleLineCommentMatcher = singleLineCommentPattern.matcher(code);
while (singleLineCommentMatcher.find()) {
System.out.println("Single-line comment: " + singleLineCommentMatcher.group(1));
}
// 匹配 -- 形式的注释
Pattern doubleDashCommentPattern = Pattern.compile("--(.*)");
Matcher doubleDashCommentMatcher = doubleDashCommentPattern.matcher(code);
while (doubleDashCommentMatcher.find()) {
System.out.println("Double-dash comment: " + doubleDashCommentMatcher.group(1));
}
}
}
```
上述代码中,`multilineCommentPattern`、`singleLineCommentPattern`和`doubleDashCommentPattern`分别用来匹配`/* */`、`//`和`--`形式的注释。其中,`multilineCommentPattern`使用了非贪婪模式,以支持匹配多行注释。
运行上述代码,输出结果如下:
```
Multiline comment: This is a
multiline
comment
Single-line comment: This is a single-line comment
Single-line comment: This is another single-line comment
Single-line comment: This is a comment
Multiline comment: This is a comment with -- in it
```
需要注意的是,如果注释符号出现在字符串或字符常量中,则不应该被视为注释。上述代码中,字符串 `str = "/* This is not a comment */"` 中的 `/*` 和 `*/` 不应该被视为注释。如果需要排除这种情况,可以使用一些复杂的正则表达式,或者在代码中添加一些特殊处理的逻辑。
JAVA正则如何使用正则匹配 “--” 和 “/**/”两种类型的注释语句,并排除字符串或字符常量的情况
你可以使用以下的正则表达式来匹配这两种类型的注释语句,并排除字符串或字符常量的情况:
`(?<!['"\s])(\/\*[\s\S]*?\*\/|--.*)`
这个正则表达式的含义是:
- `(?<!['"\s])`:负向前瞻,匹配前面不是单引号、双引号或空格的位置。
- `(\/\*[\s\S]*?\*\/|--.*)`:匹配注释语句,包括多行注释和单行注释。
- `\/\*[\s\S]*?\*\/`:匹配多行注释。
- `--.*`:匹配单行注释。
你可以将这个正则表达式编译成一个 Pattern 对象,并使用 Matcher 来查找字符串中的注释语句。示例代码如下:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String str = "/* This is a\n multi-line comment */\n" +
"int a = 1; // This is a single-line comment\n" +
"String str = \"/* This is not a comment */\";";
Pattern pattern = Pattern.compile("(?<!['\"\\s])(\\/\\*[\\s\\S]*?\\*\\/|--.*)");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
```
输出结果为:
```
/* This is a
multi-line comment */
// This is a single-line comment
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)