开发者

Regular expression dot(.) problem

If you have a string like:

"hello my name is joe bloggs! I like stuff"

How do you match just "hello my name is joe bloggs"?开发者_运维百科 I started off with:

(.+)(!)(.+)

and at point !, I want it to stop.

Please can you answer with the . function?

Thanks


You need a question mark:

(.+?)(!)(.+)


([^!]*)(!)(.*)

There could be a little difference in that . "normally" doesn't "capture" new line, while [^!] will, but unless you'll have newlines in your text, you won't see any difference.


There is not a problem with the .; you just have three capture groups: the first (.+), the (!), and the second (.+). If you just want to match a line that ends with a !, consider

(.+)!

but if you want to clearly identify each part of the line,

^(.+)!.+$

Again, the problem is not with the ., but you should have just one group surrounded by parenthesis if you want one capture.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜