作者:代码先森
转发链接:
https://mp.weixin.qq.com/s/rw29yKBwti5sIsx2GKG9pw
2025年08月12日
2025年08月12日
以下是C语言实现正则表达式的代码:
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
bool isMatch(char* s, char* p) {
int m = strlen(s);
int n = strlen(p);
// 检查模式p的合法性,避免无效的'*'
for (int i = 0; i < n; ++i) {
if (p[i] == '*' && (i == 0 || p[i-1] == '*')) {
return false;
}
}
// 创建动态规划表,dp[i][j]表示s的前i个字符和p的前j个字符是否匹配
bool *dp = (bool *)calloc((m+1) * (n+1), sizeof(bool));
if (!dp) {
return false; // 内存分配失败,但题目通常保证不会出现
}
// 初始化基础情况:空字符串匹配空模式
dp[0] = true;
// 初始化第一行,处理模式p可以匹配空字符串的情况(如"a*")
for (int j = 1; j <= n; ++j) {
if (p[j-1] == '*') {
dp[j] = dp[j-2]; // 匹配0次的情况
}
}
// 填充dp表
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p[j-1] == '*') {
// 处理'*'的情况,考虑匹配0次或多次
bool matchZero = dp[i*(n+1) + (j-2)];
bool matchMore = (p[j-2] == '.' || s[i-1] == p[j-2]) && dp[(i-1)*(n+1) + j];
dp[i*(n+1)+j] = matchZero || matchMore;
} else {
// 处理普通字符或'.'的情况
bool currentMatch = (p[j-1] == '.' || s[i-1] == p[j-1]);
dp[i*(n+1)+j] = currentMatch && dp[(i-1)*(n+1) + (j-1)];
}
}
}
bool result = dp[m*(n+1) + n];
free(dp);
return result;
}
void test(const char *s, const char *p, bool expected) {
bool result = isMatch((char*)s, (char*)p);
printf("s=\"%s\", p=\"%s\" -> %s (Expected %s)\n",
s, p,
result ? "true" : "false",
expected ? "true" : "false");
}
int main() {
// 完全匹配
test("aa", "aa", true);
// '.' 匹配任意字符
test("ab", "a.", true);
// '*' 匹配零次
test("a", "ab*", true); // b* 匹配零次
// '*' 匹配多次
test("aaa", "a*", true); // a* 匹配三次
test("ab", "a*b", true); // a* 匹配一次
// 复杂组合
test("aab", "c*a*b", true); // c* 匹配零次,a* 匹配两次
test("mississippi", "mis*is*p*.", false); // 无法匹配
// 无效模式
test("a", "a**", false); // 连续 '*' 非法
// 空字符串
test("", "", true); // 空字符串匹配空模式
test("", "a*", true); // a* 匹配零次
// 边界情况
test("a", ".*", true); // .* 匹配任意字符
return 0;
}
2025年08月12日
作者:savokiss
转载链接:
https://segmentfault.com/a/1190000021145901
在 JavaScript 中,使用 // 即可创建一个正则表达式对象,当然也可以使用 new RegExp()
2025年08月12日
●正则也叫正则表达式,又名 “规则表达式”
●正则是JS中的数据类型, 是一个复杂数据类型
●由我们自己来书写 “规则”,专门用来检测 字符串 是否符合 “规则” 使用的
2025年08月12日
来源:环球网文旅频道
On May 25th, the "2025 Chinese Brands Going Global· Worldwide Tour" kicked off in Milan, Italy, with the theme "New Quality Leads, Brands Set Sail." Under the guidance of the Consulate General of the People's Republic of China in Milan and hosted by the Global Times, the event was co-organized by Fenjiu International and the China Chamber of Commerce in Italy. It aimed to promote the stories of Chinese brand, showcase Chinese urban landscapes, and enhance cultural exchanges. Among the highlights was Nanyang, a city with over 2,800 years of urban history and nearly 7,000 years of civilization, which extended a warm welcome to global guests with an open invitation to "Visit Nanyang" and "Shop in Nanyang."
2025年08月12日
By Anthony Moretti
Children are taught at a young age that even though they might fear monsters under their bed, no such beasts will ever be found there. Perhaps parents need to reassure their kids of this fact more than once, but eventually, those young people mature sufficiently and stop fearing non-existent threats.
2025年08月12日
Delphi7通过VB6之COM对象调用PowerBASIC写的DLL功能。标题挺长,其实目标很简单,就是在Delphi7中使用PowerBASIC的MKI/CVI, MKS/CVS, MKD/CVD,并顺便加入CRC16检验函数,再进行16进制高低字节调整,方便在VB6、Delphi、Lazarus等环境下利用Modbus协议传送指令和数据时,进行十进制数的浮点转换和数据接收校验。我写的只是一个方法,其实用算法实现也并不十分复杂,但总觉得应该让曾经精典的老古懂们能做点事情。