1.邊界

/\bapple/

 

result:

i am apple 

i am appleabcddd

 

fault:

i am capple

2.括號

/(foo){1.2}/  foo出現1,2 次就符合

/foo{1,2}/  第二個o出現1,2次就符合

3.

http://larry850806.github.io/2016/06/23/regex/

4.

\s空白字元[ \r\t\n\f]\S非空白字元[^ \r\t\n\f]

5.?: 不捕捉

down voteaccepted

Let me try to explain this with an example.

Consider the following text:

https://stackoverflow.com/
https://stackoverflow.com/questions/tagged/regex

Now, if I apply the regex below over it...

(https?|ftp)://([^/\r\n]+)(/[^\r\n]*)?

... I would get the following result:

Match "https://stackoverflow.com/"
     Group 1: "http"
     Group 2: "stackoverflow.com"
     Group 3: "/"

Match "https://stackoverflow.com/questions/tagged/regex"
     Group 1: "http"
     Group 2: "stackoverflow.com"
     Group 3: "/questions/tagged/regex"

But I don't care about the protocol -- I just want the host and path of the URL. So, I change the regex to include the non-capturing group (?:).

(?:https?|ftp)://([^/\r\n]+)(/[^\r\n]*)?

Now, my result looks like this:

Match "https://stackoverflow.com/"
     Group 1: "stackoverflow.com"
     Group 2: "/"

Match "https://stackoverflow.com/questions/tagged/regex"
     Group 1: "stackoverflow.com"
     Group 2: "/questions/tagged/regex"

See? The first group has not been captured. The parser uses it to match the text, but ignores it later, in the final result.

 

arrow
arrow
    全站熱搜

    學習程式 發表在 痞客邦 留言(0) 人氣()