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
| import 'package:flutter/material.dart';
void main() { debugPrint("1313");
debugPrint("--------1----------"); debugPrint(RegUtil.convert1('# abcd')); debugPrint(RegUtil.convert1('## abcd'));
debugPrint("--------2----------"); debugPrint(RegUtil.convert2("# [[abcd]]")); debugPrint(RegUtil.convert2("## [[abcd]]")); debugPrint(RegUtil.convert2("### [[lmn]]"));
debugPrint("--------3----------"); debugPrint(RegUtil.convert3("abcd\"*lmn"));
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 { 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'; }); }
static String convert2(String text) { final pattern = RegExp(r'^#+ \[\[(.+)\]\]$', multiLine: true); return pattern.allMatches(text).fold('', (result, match) { if (match.group(0) == null) { return ''; } const level = '#'; final content = match.group(1); return '$result$level $content\n'; }); }
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'; return input.replaceAll(pattern, output); }
static String convert4_1(String text) {
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; }
static String convert4_2(String text) { 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; }
static String convert4(String text) { 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(); } }
|