快速正则表达式问题

编程入门 行业动态 更新时间:2024-10-25 04:21:57
本文介绍了快速正则表达式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前我的正则表达式是你好 我怎么能允许你好或你好,即。在单词之后或者字符之后必须有任何内容,它可能只是一个空格。 谢谢, 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?

更多推荐

快速正则表达式问题

本文发布于:2023-10-24 09:34:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快速   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!