正则表达式匹配第一个期间的所有内容 [英] Regex match everything up to first period
本文介绍了正则表达式匹配第一个期间的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试对所有内容进行惰性正则表达式匹配,直到句子的第一个句点.
Trying to get a lazy regex match of everything up until the first period of a sentence.
例如只想得到杰克和吉尔".从这句话:
杰克和吉尔.上山去取一个桶."
e.g. Just want to get "jack and jill." from this sentence:
"jack and jill. went up the hill. to fetch a pail."
/.+\./
匹配整个句子(example)/(.+?\.)/
匹配每个实例(example)
/.+\./
matches the whole sentence (example)
/(.+?\.)/
matches each instance (example)
有没有办法只匹配第一个实例?
Is there a way to just match the first instance?
推荐答案
/^([^.]+)/
让我们分解一下,
^
是换行符
[^.]
匹配任何不是句点的字符
[^.]
this matches any character that's not a period
\+
使用一段时间
并且用()封装表达式来捕获它.
And the expression is encapsulated with () to capture it.
这篇关于正则表达式匹配第一个期间的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文