VB.NET回顾我的正则表达式(我自己的学习目的)尽量不要贪心(VB.NET Review my regex (My own learnining purpose) Trying not to be

编程入门 行业动态 更新时间:2024-10-27 10:30:37
VB.NET回顾我的正则表达式(我自己的学习目的)尽量不要贪心(VB.NET Review my regex (My own learnining purpose) Trying not to be greedy)

我上次开始说请不要建议其他Net方法,因为我想学习正则表达式。 这纯粹是为了我自己的学习,所以这里。

贪婪可能有用,但并不总是答案,我想可能会导致未来的问题和糟糕的结果。 以这个示例文本为例,是的,这是我刚才玩的一种不寻常的格式

{regex} Hello World GET a1b2c3 {regex}

我所追求的部分是“a1b2c32”,它只是一个字母或数字,但总是以{和一个空格开头。 我的第一次尝试是积极的看法。

(?<={regex}Hello World GET )[a-z0-9]+(?={regex})

但后来我争取匹配任何不是大括号的字符

"Hello World GET [^{]+"

我确实意识到这将捕获我不想要的文本并引用到group1中。 基本上我只是在寻找第一个例子的改进,如果可以的话。 这一切都与我学习和测试有关,所以这不是真正的生活项目。

谢谢大家花时间阅读:)

as last time i will start by saying please don't suggest other Net methods because i am wanting to learn regex. This is purely for my own learning so here goes.

Being greedy may work but it's not always the answer and i guess could cause future issues and bad results. Take this example text, and yes it is an unusual format i am just playing

{regex}Hello World GET a1b2c3{regex}

The part i am after is "a1b2c32" which would only ever be a letter or number but would always end with a { and start with a white space. My first attempt was a positive look a head.

(?<={regex}Hello World GET )[a-z0-9]+(?={regex})

But then i fought about matching any char that is not a curly bracket

"Hello World GET [^{]+"

I do realize this would capture the text which i dont want and reference into group1. Basically i am just looking for improvements to my first example if any can be made. It's all about learning and testing for me so it's no real life project.

Thank you all for taking the time to read :)

最满意答案

也许更普遍的正则表达式:

[^{ ]+(?={)

regex101演示

[^{ ]+匹配除{和空格之外的任何字符, +确保至少有一个字符,这样就不会匹配字符串最开头的空字符串。

积极的前瞻不应该难以理解。

Maybe a more general regex:

[^{ ]+(?={)

regex101 demo

[^{ ]+ matches any character except { and space and the + ensures that there's at least one character so that you don't match the empty string at the very start of the string.

The positive lookahead shouldn't be difficult to understand.

更多推荐

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

发布评论

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

>www.elefans.com

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