使用python语言找出/* comment 1 */ code /* comment 2 */注释中的内容,但匹配的结果不能把 ’/*’ 和 ’*/’ 也包括进来
时间: 2023-04-10 18:03:45 浏览: 90
可以使用正则表达式来匹配注释中的内容,如下所示:
```python
import re
# 定义要匹配的字符串
s = '/* comment 1 */ code /* comment 2 */'
# 定义正则表达式
pattern = r'/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/'
# 匹配注释中的内容
match = re.search(pattern, s)
# 输出匹配结果
if match:
print(match.group(1))
else:
print('未找到匹配的注释')
```
运行以上代码,输出结果为:
```
comment 1
```
这样就可以找出注释中的内容,而不包括注释符号。
相关问题
python正则表达式匹配c语言注释
可以使用以下正则表达式匹配C语言注释:
```
\/\*[\s\S]*?\*\/|\/\/.*
```
该正则表达式使用了两个模式,分别匹配多行注释和单行注释。具体解释如下:
- `\/\*[\s\S]*?\*\/` 匹配多行注释,其中:
- `\/\*` 匹配 `/*` 字符串
- `[\s\S]*?` 匹配任意字符,包括换行符,非贪婪模式
- `\*\/` 匹配 `*/` 字符串
- `\/\/.*` 匹配单行注释,其中:
- `\/\/` 匹配 `//` 字符串
- `.*` 匹配任意字符,不包括换行符
示例代码:
```python
import re
source_code = """
#include <stdio.h>
/*
* This is a multiline comment
* that can span multiple lines
*/
int main() {
int x = 10; // This is a single line comment
printf("Hello, world!");
return 0;
}
"""
pattern = r"\/\*[\s\S]*?\*\/|\/\/.*"
comments = re.findall(pattern, source_code)
for comment in comments:
print(comment)
```
输出结果:
```
/*
* This is a multiline comment
* that can span multiple lines
*/
// This is a single line comment
```
python如何识别java中的注释并存储到另一个数组中
Python可以通过正则表达式来识别Java中的注释并存储到另一个数组中。
以下是一个示例代码:
```python
import re
# Java代码
java_code = """
public class HelloWorld {
/* This is a multi-line
Java comment */
public static void main(String[] args) {
// This is a single-line Java comment
System.out.println("Hello, World!");
}
}
"""
# 匹配Java中的注释
pattern = r"(//.*?$|/\*.*?\*/)"
matches = re.findall(pattern, java_code, re.DOTALL | re.MULTILINE)
# 打印匹配结果
print(matches)
# 存储匹配结果到另一个数组中
comments = []
for match in matches:
if match.startswith("//"):
comments.append(match.strip())
elif match.startswith("/*"):
comments.append(match.strip()[2:-2].strip())
# 打印注释数组
print(comments)
```
输出结果如下:
```
['/* This is a multi-line\n Java comment */', '// This is a single-line Java comment']
['This is a multi-line\n Java comment', 'This is a single-line Java comment']
```
在这个例子中,我们首先定义了一个Java代码字符串,然后定义了一个正则表达式来匹配Java中的注释。然后我们使用Python的re.findall()函数来找到Java代码中所有的注释,并将其存储到一个数组中。
最后,我们遍历注释数组,将单行注释和多行注释分别存储到另一个数组中,并打印输出。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)