Python入门

[toc]

正则表达式示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// main.dart

import 'package:flutter/material.dart';

void main() {
debugPrint("1313");
// runApp(const MyApp());

debugPrint("--------1----------");
debugPrint(RegUtil.convert1('# abcd'));
debugPrint(RegUtil.convert1('## abcd'));
// debugPrint(RegUtil.convert1('# abcd\n## efgh'));

debugPrint("--------2----------");
debugPrint(RegUtil.convert2("# [[abcd]]"));
debugPrint(RegUtil.convert2("## [[abcd]]"));
debugPrint(RegUtil.convert2("### [[lmn]]"));

debugPrint("--------3----------");
debugPrint(RegUtil.convert3("abcd\"*lmn")); // todo

debugPrint("--------4----------");
debugPrint(RegUtil.convert4(
"Introducing ChatGPT We've trained (a model) called #ChatGPT which interacts in a (conversational way). The # dialogue format makes it possible for ChatGPT to answer followup questions, admit its mistakes, challenge incorrect premises, and reject inappropriate requests."));

debugPrint("--------5----------");
debugPrint(
RegUtil.convert5("你的好朋友\$张三\$发来⼀个地址\$https://www.google.com\$,你想要打开吗?")
.toString());
debugPrint(
RegUtil.convert5("你的好朋友\$李四\$发来⼀个地址\$http://www.google.com\$,你想要打开吗?")
.toString());
}

class RegUtil {
// convert "# abcd" to "# [[abcd]]"
// convert "## abcd" to "## [[abcd]]"
static String convert1(String text) {
final pattern = RegExp(r'^(#+) (.+)$', multiLine: true);
return pattern.allMatches(text).fold('', (result, match) {
final level = match.group(1);
final content = match.group(2)?.trim();
return '$result$level [[$content]]\n';
});
}

// convert "# [[abcd]]" to "# abcd"
// convert "## [[abcd]]" to "# abcd"
// convert "### [[lmn]]" to "# lmn
static String convert2(String text) {
final pattern = RegExp(r'^#+ \[\[(.+)\]\]$', multiLine: true);
return pattern.allMatches(text).fold('', (result, match) {
if (match.group(0) == null) {
return '';
}
// final level = '#' * match.group(0)!.indexOf(' ');
const level = '#';
final content = match.group(1);
return '$result$level $content\n';
});
}

// convert `abcd"*lmn` to `abcd'lmn.com`
static String convert3(String text) {
text = 'abcd"*lmn';

String output = text.replaceAll(RegExp('"'), "'");
output = output.replaceAll(RegExp(r'\*'), "");
output += ".com";
return output;
}

static String convert32(String input) {
RegExp pattern = RegExp(r'"\*([\w]+)');
String? output = pattern.stringMatch(input);
if (output == null) {
return input;
}
output = output.replaceAll('"', '\''); // 将双引号替换为单引号
output = output.replaceAll('*', ''); // 去掉星号
output += '.com'; // 添加 .com 后缀
return input.replaceAll(pattern, output);
}

// 输出 "a model"
// 怎么获取 trained (a model) in a (conversational way). The 中的a model,但不会获取到conversational way,用dart语言和正则。
// 即我希望获取()中的字符,但如果这些字符所在的()的前面有多个空格就不要(提取的是括号中的内容,但只想要获取前面没有多个空格的内容)
static String convert4_1(String text) {
// RegExp regex = RegExp(r'(?:\(|#)\s*(\w+(?:\s+\w+)*)\)');
// RegExp regex = RegExp(r'(?:\()\s*(\w+(?:\s+\w+)*)\)');
// Iterable<Match> matches = regex.allMatches(text);
// List<String?> results = matches.map((match) {
// final level = match.group(1);
// // final content = match.group(2)?.trim();
// return '$level';
// }).toList();

String text = "trained (a model) in a (conversational way). The";
RegExp regExp = RegExp(r"\(\s*(?<!\s{2,}).*?\s*\)");
Iterable<Match> matches = regExp.allMatches(text);
String word = "";
for (Match match in matches) {
String matchWord = match.group(0) ?? '';
word = matchWord.trim().substring(1, matchWord.length - 1);
break;
}
debugPrint(word);
return word;
}

// 输出 "ChatGPT"
// 怎么获取 The # dialogue format makes #ChatGPT it 中的ChatGPT,但不会获取到dialogue,用dart语言和正则。
// 即我希望获取#后的单词,但如果这个单词和#号之间有空格,就不要
static String convert4_2(String text) {
// String text = "The # dialogue format makes #ChatGPT it";
RegExp regExp = RegExp(r"#\s*(\w+)\b");
Iterable<Match> matches = regExp.allMatches(text);
String word = "";
for (Match match in matches) {
String matchWord = match.group(1) ?? '';
if (!matchWord.contains(" ")) {
word = matchWord;
break;
}
}
debugPrint(word);
return word;
}

// conversational way is invalid since there are more than 1 space before it, dialogure is invalid since there is a space after #
static String convert4(String text) {
// String text = "trained (a model) in a (conversational way). The # dialogue format makes #ChatGPT it";
RegExp regExp = RegExp(r"\((\s*(?<!\s{2,}).*?\s*)\)|#\s*(\w+)\b");
List<String> array = [];
Iterable<Match> matches = regExp.allMatches(text);
for (Match match in matches) {
if (match.group(1) != null) {
String model = match.group(1)!.trim();
array.add(model);
}
if (match.group(2) != null) {
String word = match.group(2) ?? '';
array.add(word);
break;
}
}

return array.toString();
}

static String convert5(String text) {
RegExp nameRegex = RegExp(r'\$(.*?)\$');
RegExp urlRegex = RegExp(r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+');
String? name = nameRegex.stringMatch(text)?.replaceAll('\$', '');
String? url = urlRegex.stringMatch(text);

Map<String, dynamic> friend = {"name": name, "url": url};

return friend.toString();
}
}

JS语言的实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<script>


var str = "abcd test@runoo3b.com 1234";
var patt1 = /\b[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,6}\b/g;
document.write(str.match(patt1));

function convert1(text) {
const pattern = /^(#+) (.+)$/gm;
return text.replace(pattern, function(match, p1, p2) {
const level = p1;
const content = p2.trim();
return level + ' [[' + content + ']]\n';
});
}

function convert2(text) {
const pattern = /^#+ \[\[(.+)\]\]$/gm;
return text.replace(pattern, function(match, p1) {
const level = '#';
const content = p1;
return level + ' ' + content + '\n';
});
}

function convert3(text) {
let output = text.replaceAll('"', "'");
output = output.replaceAll('\*', "");
output += ".com";
return output;
}

function convert4(text) {
const regExp = /\((\s*(?<!\s{2,}).*?\s*)\)|#\s*(\w+)\b/g;
const array = [];
let matches = text.matchAll(regExp);
for (let match of matches) {
if (match[1] !== undefined) {
let model = match[1].trim();
array.push(model);
}
if (match[2] !== undefined) {
let word = match[2];
array.push(word);
break
}
}
return array.toString();
}

function convert5(text) {
const nameRegex = /\$(.*?)\$/;
const urlRegex = /https?:\/\/(?:[-\w.]|(?:%[\da-fA-F]{2}))+/;
let name = nameRegex.exec(text)?.[1].replace(/$/g, '');
let url = urlRegex.exec(text)?.[0];
let friend = {"name": name, "url": url};
return JSON.stringify(friend);
}


document.write(convert1("# abcd"));
document.write(convert2("# [[abcd]]"));
document.write(convert3('abcd"*lmn'));
document.write(convert4("Introducing ChatGPT We've trained (a model) called #ChatGPT which interacts in a (conversational way). The # dialogue format makes it possible for ChatGPT to answer followup questions, admit its mistakes, challenge incorrect premises, and reject inappropriate requests."));
document.write(convert5("你的好朋友\$张三\$发来⼀个地址\$https://www.google.com\$,你想要打开吗?"));
</script>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

function convert1(text) {
const pattern = /^(#+) (.+)$/gm;
return text.replace(pattern, function(match, p1, p2) {
const level = p1;
const content = p2.trim();
return level + ' [[' + content + ']]';
});
}

function convert2(text) {
const pattern = /^#+ \[\[(.+)\]\]$/gm;
return text.replace(pattern, function(match, p1) {
const level = '#';
const content = p1;
return level + ' ' + content ;
});
}

function convert3(text) {
if (typeof text != 'string') {
return "";
}
let output = text.replace(/"/g, "'");
output = output.replace(/\*/g, "");

output += ".com";
return output;
}

function convert4(text) {
const regExp = /\((\s*(?<!\s{2,}).*?\s*)\)|#\s*(\w+)\b/g;
const array = [];
let matches = text.matchAll(regExp);
for (let match of matches) {
if (match[1] !== undefined) {
let model = match[1].trim();
array.push(model);
}
if (match[2] !== undefined) {
let word = match[2];
array.push(word);
break
}
}
return array.toString();
}

function convert5(text) {
const nameRegex = /\$(.*?)\$/;
const urlRegex = /https?:\/\/(?:[-\w.]|(?:%[\da-fA-F]{2}))+/;

let name = nameRegex.exec(text) ? nameRegex.exec(text)[1].replace(/\$/g, '') : null;
let url = urlRegex.exec(text) ? urlRegex.exec(text)[0] : null;

let friend = {"name": name, "url": url};
return JSON.stringify(friend);
}



console.log(convert1("# abcd"));
console.log(convert2("# [[abcd]]"));
console.log(convert3('abcd"*lmn'));
console.log(convert4("Introducing ChatGPT We've trained (a model) called #ChatGPT which interacts in a (conversational way). The # dialogue format makes it possible for ChatGPT to answer followup questions, admit its mistakes, challenge incorrect premises, and reject inappropriate requests."));
console.log(convert5("你的好朋友\$张三\$发来⼀个地址\$https://www.google.com\$,你想要打开吗?"));