示例代码 以下PHP代码演示了如何使用这个正则表达式来提取数字:<?php function extractNumberAtEnd(string $str): ?int { // 定义正则表达式: // ^ - 匹配字符串开头 // \S - 匹配一个非空白字符(防止开头是空格的字符串) // .* - 匹配任意字符(除换行符)零次或多次 // ' ' - 匹配一个字面空格 // (\b\d+) - 捕获组:\b(单词边界)和 \d+(一个或多个数字) // $ - 匹配字符串结尾 if (preg_match("/^\S.* (\b\d+)$/", $str, $matches)) { // preg_match会将完整的匹配结果放在$matches[0] // 捕获组的结果从$matches[1]开始 // end($matches) 是获取最后一个捕获组的便捷方式 return (int)end($matches); } return null; // 如果没有匹配到,则返回null } // 测试用例 $str1 = "1234 lkjsdhf ldjfh 1223"; $str2 = "a b 1212"; $str3 = "a 1212"; $str4 = "another file name 9988"; $str5 = " 1212"; // 不应匹配 $str6 = "abc1234"; // 不应匹配 $str7 = "onlytext"; // 不应匹配 $str8 = "text with space but no number "; // 不应匹配 echo "字符串: '{$str1}' -> 提取结果: " . (extractNumberAtEnd($str1) ?? "未匹配") . "\n"; echo "字符串: '{$str2}' -> 提取结果: " . (extractNumberAtEnd($str2) ?? "未匹配") . "\n"; echo "字符串: '{$str3}' -> 提取结果: " . (extractNumberAtEnd($str3) ?? "未匹配") . "\n"; echo "字符串: '{$str4}' -> 提取结果: " . (extractNumberAtEnd($str4) ?? "未匹配") . "\n"; echo "字符串: '{$str5}' -> 提取结果: " . (extractNumberAtEnd($str5) ?? "未匹配") . "\n"; echo "字符串: '{$str6}' -> 提取结果: " . (extractNumberAtEnd($str6) ?? "未匹配") . "\n"; echo "字符串: '{$str7}' -> 提取结果: " . (extractNumberAtEnd($str7) ?? "未匹配") . "\n"; echo "字符串: '{$str8}' -> 提取结果: " . (extractNumberAtEnd($str8) ?? "未匹配") . "\n"; ?>输出结果:字符串: '1234 lkjsdhf ldjfh 1223' -> 提取结果: 1223 字符串: 'a b 1212' -> 提取结果: 1212 字符串: 'a 1212' -> 提取结果: 1212 字符串: 'another file name 9988' -> 提取结果: 9988 字符串: ' 1212' -> 提取结果: 未匹配 字符串: 'abc1234' -> 提取结果: 未匹配 字符串: 'onlytext' -> 提取结果: 未匹配 字符串: 'text with space but no number ' -> 提取结果: 未匹配注意事项 end($matches) 的使用: preg_match 函数会将所有匹配到的内容存储在 $matches 数组中。
nil 是特定类型的“未初始化”状态,而零值是所有类型的默认初始值。
最常见的,也是我当初遇到的,就是“输入验证”。
回滚不是“补救措施”,而是部署流程的必要组成部分。
调用时,编译器会自动匹配最合适的版本。
析构顺序问题:若单例依赖其他全局对象,需注意析构顺序。
本教程旨在解决PySimpleGUI Listbox在动态添加数据时滚动条自动跳回顶部的问题。
使用时要清楚它的边界,尤其在高性能库开发中更要谨慎设计数据流。
示例:查找并返回指定值 结合 in_array() 和三元运算符,我们可以简洁地实现查找指定值并根据结果返回该值或 NULL 的逻辑。
// 假设 get_user_id 返回 std::optional<int> // get_user_name 返回 std::optional<std::string> // find_profile 返回 std::optional<Profile> auto profile = get_user_id() .and_then([](int id){ return get_user_name(id); }) // 如果有id,继续获取name .and_then([](const std::string& name){ return find_profile(name); }); // 如果有name,继续查找profile if (profile) { profile->display(); }这是一种非常优雅的处理流程,避免了层层嵌套的 if 检查。
要创建列表的独立副本,可以使用切片、list() 构造函数或 copy 模块。
对于InnoDB引擎,MySQL 5.6及以上版本支持FULLTEXT索引。
本文探讨Go语言中因使用过时ODBC包导致的编译错误,并详细介绍Go标准库database/sql及其驱动模型。
因此,new(Rectangle)可以合法地赋值给Shaper类型的变量shape。
基本上就这些。
可通过.tool-versions(配合asdf)或CI脚本锁定版本:# in CI go version | grep "go1.21.5"基本上就这些。
34 查看详情 fmt.Println(math.Pow(2, 3)) // 输出: 8 fmt.Println(math.Sqrt(16)) // 输出: 4 fmt.Println(math.Exp(1)) // 输出: 2.718281828459045 fmt.Println(math.Log(math.E)) // 输出: 1 三角函数与反三角函数 输入单位为弧度,非角度: math.Sin(x), math.Cos(x), math.Tan(x) math.Asin(x), math.Acos(x), math.Atan(x) math.Atan2(y, x):返回从原点到点 (x,y) 的向量与 x 轴的夹角 示例: 立即学习“go语言免费学习笔记(深入)”; angle := math.Pi / 4 fmt.Println(math.Sin(angle)) // 输出: 0.7071067811865475 fmt.Println(math.Cos(angle)) // 输出: 0.7071067811865476 fmt.Println(math.Atan2(1, 1)) // 输出: 0.7853981633974483 (即 π/4) 取整与符号处理 常见取整方式: math.Floor(x):向下取整 math.Ceil(x):向上取整 math.Round(x):四舍五入(Go 1.10+) math.Trunc(x):截断小数部分 符号与绝对值: math.Abs(x):返回绝对值 math.Copysign(x, y):返回 |x| 但带 y 的符号 示例: 立即学习“go语言免费学习笔记(深入)”; fmt.Println(math.Floor(3.7)) // 输出: 3 fmt.Println(math.Ceil(3.2)) // 输出: 4 fmt.Println(math.Round(3.5)) // 输出: 4 fmt.Println(math.Abs(-5.5)) // 输出: 5.5 最大值、最小值与特殊值判断 比较函数: math.Max(x, y):返回较大值 math.Min(x, y):返回较小值 判断特殊浮点值: math.IsNaN(x):是否为 NaN math.IsInf(x, sign):判断是否无穷大(sign=1正无穷,-1负无穷,0则任意) 示例: 立即学习“go语言免费学习笔记(深入)”; fmt.Println(math.Max(2, 5)) // 输出: 5 fmt.Println(math.Min(-1.5, -3.2)) // 输出: -3.2 nan := math.NaN() fmt.Println(math.IsNaN(nan)) // 输出: true 基本上就这些。
.replace():这是字符串替换方法。
阿里云-虚拟数字人 阿里云-虚拟数字人是什么?
PHP 脚本的独立实例: 对于每个 HTTP 请求,服务器都会启动一个新的 PHP 脚本实例来处理该请求。
本文链接:http://www.komputia.com/202620_980727.html