gpt4 book ai didi

正则表达式:匹配到第一次出现的字符

转载 作者:行者123 更新时间:2023-12-05 01:28:21 27 4
gpt4 key购买 nike

我正在寻找匹配所有内容的模式 直到 特定字符的第一次出现,比如“;” - 一个 分号 .

我是这样写的:

/^(.*);/

但它实际上匹配所有内容(包括分号),直到最后一次出现分号。

最佳答案

你需要

/[^;]*/
[^;]是一个字符类,它匹配除分号以外的所有内容。

引用 perlre 联机帮助页:

You can specify a character class, by enclosing a list of characters in [] , which will match any character from the list. If the first character after the "[" is "^", the class matches any character not in the list.



这应该适用于大多数正则表达式方言。

关于正则表达式:匹配到第一次出现的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2013124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com