简体   繁体   English

试图理解.NET正则表达式

[英]Trying to understand .NET regular expressions

I've been doing a lot of reading on .NET regular expressions and I have developed a regular expression, that I can't make any sense of. 我一直在做很多关于.NET正则表达式的阅读,我已经开发了一个正则表达式,我无法理解。

(src|href)="\w+|(\w+/)+

The way I read this regular expression: 我读这个正则表达式的方式:

  1. Match exactly "src" or "href" 完全匹配“src”或“href”
  2. Followed by =" 其次是=“
  3. Followed by match 1 or more word characters ([a-zA-Z0-9_]) or one or more of (one or more word characters followed by /) 接下来匹配1个或多个单词字符([a-zA-Z0-9_])或一个或多个(一个或多个单词字符后跟/)

This is meant to match something like 'src="Folder', 'src="folder/', 'href="Folder/SubFolder/', etc. 这是为了匹配'src =“Folder','src =”folder /','href =“Folder / SubFolder /'等。

Input: 输入:

<!DOCTYPE html> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns =“http://www.w3.org/1999/xhtml”>
<head> <HEAD>

Using this regular expression, with this input, there is one match. 使用此正则表达式,使用此输入,有一个匹配项。

org/1999/ 组织/ 1999 /

Can anyone possibly explain this? 任何人都可以解释一下吗? Src or href aren't referenced in the entire string, how can there be any match at all? 整个字符串中没有引用Src或href,怎么会有任何匹配?

What's happening here is the | 这里发生的是| is seperating the regex into two completely seperate conditions. 正在将正则表达式分成两个完全独立的条件。 That is select either: (src|href)="\\w+ OR (\\w+/)+ of which second bit is being matched: 那就是选择: (src|href)="\\w+ OR (\\w+/)+ ,其中匹配第二位:

org/1999/

In your case you'd probably need to put the last part in parentheses to make it clear what exactly the alternation | 在你的情况,你很可能需要把最后一部分括号要清楚究竟交替| refers to: 指:

(src|href)="(\w+|(\w+/)+)

Btw I used Expresso to help work this out. 顺便说一句,我用Expresso来帮助解决这个问题。

Try Expresso , for example. 例如,试试Expresso It has a nice "explain" feature. 它有一个很好的“解释”功能。

Try this app http://www.regexbuddy.com/ . 试试这个应用程序http://www.regexbuddy.com/ You can set the RegEx flavor to .NET and it has a great tab which breaks down each element of your RegEx. 您可以将RegEx风格设置为.NET,它有一个很好的选项卡,可以分解您的RegEx的每个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM