快速正则表达式问题 [英] Quick regex question

查看:102
本文介绍了快速正则表达式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





目前我的正则表达式是你好



我怎么能允许你好或你好,即。在单词之后或者字符之后必须有任何内容,它可能只是一个空格。



谢谢,



Rix



编辑:



我现在有

(([。])|([]))hello \\\ | \(hello \)





匹配:

.hello。

你好

(你好)

但不是x你好



我以前怎样才能允许任何事情?



我尝试了什么:



查找正则表达式而不理解模式

解决方案

试试这个:

 hello \s?

\s一个空格

?重复零或一次


转到RegExr:学习,构建和测试RegEx [ ^ ]。


TRo加入Pete Leow的建议:你好不符合只是你好,而hello \s?不仅仅是将hello与后面的空格匹配。它也匹配

你好那里
你好 - 那里
why-hello!

和在该序列中连续包含字母hello的任何其他内容。

如果您想完全匹配hello或hello,那么您需要:

 ^ hello\s?

Hi,

Currently my regex pattern is "hello"

How can I allow either "hello" or "hello ", ie. there must be either nothing after the word or the character directly after it may only be a space.

Thanks,

Rix

EDIT:

I now have

(([."])|([ ]))hello\2|\(hello\)



which matches:
.hello.
"hello"
(hello)
but not x hello

How can I allow anything before?

What I have tried:

Looking up regex and not understanding the patterns

解决方案

Try this:

hello\s?

\s for one space
? repeat zero or once


Go to RegExr: Learn, Build, & Test RegEx[^].


TRo add to Pete Leow's suggestion: "hello" doesn't match just "hello", and "hello\s?" doesn't just match hello with a space after it. It also matches

"hello there"
"hello-there"
"why-hello!"

And anything else which contains the letters "hello" contiguously in that sequence.
If you want to match exactly "hello" or "hello " then you want:

^hello\s?


这篇关于快速正则表达式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆