기초 지식 - Golang 의 정규 표현 식

14234 단어
------------------------------------------------------------

Golang       

------------------------------------------------------------

  :

------------------------------

  :

        .                           ,     s = true,        

        [   ]              “   ”      ,“   ”      
        [^   ]             “   ”      ,“   ”      

        \  Perl           “Perl ”      ,“Perl ”      
        \  Perl           “Perl ”      ,“Perl ”      

        [:ASCII  :]         “ASCII ”      ,“ASCII ”      
        [:^ASCII  :]        “ASCII ”      ,“ASCII ”      

        \pUnicode         “Unicode ”      (    ),“Unicode ”      
        \PUnicode         “Unicode ”      (    ),“Unicode ”      

        \p{Unicode  }       “Unicode ”      ,“Unicode ”      
        \P{Unicode  }       “Unicode ”      ,“Unicode ”      

------------------------------

  :

        xy                xy(x      y)
        x|y               x   y (     x)

------------------------------

  :

        x*                     x,      (  )
        x+                     x,      (  )
        x?                     x,      (  )
        x{n,m}            n   m   x,      (  )
        x{n,}             n      x,      (  )
        x{n}               n   x
        x*?                    x,      (   )
        x+?                    x,      (   )
        x??                    x,      (   )
        x{n,m}?           n   m   x,      (   )
        x{n,}?            n      x,      (   )
        x{n}?              n   x

------------------------------

  :

        (    )                 ,      (   )
        (?P    )         ,          (   )
        (?:    )                (   )
        (?  )                      ,   ,              
        (?  :    )             ,   ,             

              :
        xyz  (   xyz   )
        -xyz (   xyz   )
        xy-z (   xy   ,    z   )

                :
        i                     (    false)
        m                  :  ^   $             ,       (    false)
        s                .    
( false) U : x* x*? ( false) ------------------------------ : ^ m=true , (m false) $ m=true , (m false) \A , m \b \B \z , m ------------------------------ : \a ( \x07) : \b , \b , \x08 。 \f ( \x0C) \t ( \x09)
( \x0A) \r ( \x0D) \v ( \x0B) \123 8 ( 3 ) \x7F 16 ( 3 ) \x{10FFFF} 16 ( 10FFFF ) \Q...\E \Q \E , \\ \ \^ ^ \$ $ \. . \* * \+ + \? ? \{ { \} } \( ( \) ) \[ [ \] ] \| | ------------------------------ “ ” “ ” : [\d] ( \d) [^\d] ( \D) [\D] ( \D) [^\D] ( \d) [[:name:]] “ASCII ” “ ” ( [:name:]) [^[:name:]] “ASCII ” “ ” ( [:^name:]) [\p{Name}] “Unicode ” “ ” ( \p{Name}) [^\p{Name}] “Unicode ” “ ” ( \P{Name}) ------------------------------------------------------------ : ------------------------------ “ ” (“ ” “Perl ”、“ASCII ”、“Unicode ”): x A-Z ( ) \ Perl [:ASCII :] ASCII \p{Unicode } Unicode ( ) \pUnicode Unicode ( ) ------------------------------ “Perl ” : \d ( [0-9]) \D ( [^0-9]) \s ( [\t
\f\r ]) \S ( [^\t
\f\r ]) \w ( [0-9A-Za-z_]) \W ( [^0-9A-Za-z_]) ------------------------------ “ASCII ” [:alnum:] ( [0-9A-Za-z]) [:alpha:] ( [A-Za-z]) [:ascii:] ASCII ( [\x00-\x7F]) [:blank:] ( [\t ]) [:cntrl:] ( [\x00-\x1F\x7F]) [:digit:] ( [0-9]) [:graph:] ( [!-~]) [:lower:] ( [a-z]) [:print:] ( [ -~] [ [:graph:]]) [:punct:] ( [!-/:-@[- {-~]) [:space:] ( [\t
\v\f\r ]) [:upper:] ( [A-Z]) [:word:] ( [0-9A-Za-z_]) [:xdigit:] 16 ( [0-9A-Fa-f]) ------------------------------ “Unicode ” --- : C - - (other) Cc (control) Cf (format) Co (private use) Cs (surrogate) L - - (letter) Ll (lowercase letter) Lm (modifier letter) Lo (other letter) Lt (titlecase letter) Lu (uppercase letter) M - - (mark) Mc (spacing mark) Me (enclosing mark) Mn (non-spacing mark) N - - (number) Nd (decimal number) Nl (letter number) No (other number) P - - (punctuation) Pc (connector punctuation) Pd (dash punctuation) Pe (close punctuation) Pf (final punctuation) Pi (initial punctuation) Po (other punctuation) Ps (open punctuation) S - - (symbol) Sc (currency symbol) Sk (modifier symbol) Sm (math symbol) So (other symbol) Z - - (separator) Zl (line separator) Zp (paragraph separator) Zs (space separator) ------------------------------ “Unicode ” --- : Arabic Armenian Balinese Bengali Bopomofo Braille Buginese Buhid Canadian_Aboriginal Carian Cham Cherokee Common , Coptic Cuneiform Cypriot Cyrillic Deseret Devanagari Ethiopic Georgian Glagolitic Gothic Greek Gujarati Gurmukhi Han Hangul Hanunoo Hebrew Hiragana ( ) Inherited Kannada Katakana ( ) Kayah_Li Kharoshthi Khmer Lao Latin Lepcha Limbu Linear_B B ( ) Lycian Lydian Malayalam Mongolian Myanmar New_Tai_Lue Nko Nko Ogham Ol_Chiki Old_Italic Old_Persian Oriya Osmanya Phags_Pa Phoenician Rejang Runic Saurashtra ( ) Shavian Sinhala Sundanese Syloti_Nagri Syriac Tagalog Tagbanwa Tai_Le Tamil Telugu Thaana Thai Tibetan Tifinagh Ugaritic Vai Yi ------------------------------------------------------------ :    [a-z] , [] - , - [] , [-a-z] [a-z-]    [] :\f、\t、
、\r、\v、\377、\xFF、\x{10FFFF}、\\、\^、\$、\.、\*、\+、\?、\{、\}、\(、\)、\[、\]、\|( )    , ,“ ” $1、${1}、$name、${name} “ ” 。 $0 ,$1 1 ,$2 2 ,……。    “ ” $name , ,name , :$1x ${1x}, ${1}x, :$10 ${10}, ${1}0。    $ , “ ” $ , \$ 。    “Perl ”, “Perl ” ,Go “POSIX ”,“POSIX ” “Perl ” , 。 ------------------------------------------------------------ // func main() { text := `Hello !123 Go.` // reg := regexp.MustCompile(`[a-z]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["ello" "o"] // reg = regexp.MustCompile(`[^a-z]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["H" " !123 G" "."] // reg = regexp.MustCompile(`[\w]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello" "123" "Go"] // 、 reg = regexp.MustCompile(`[^\w\s]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // [" !" "."] // reg = regexp.MustCompile(`[[:upper:]]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["H" "G"] // ASCII reg = regexp.MustCompile(`[[:^ascii:]]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // [" !"] // reg = regexp.MustCompile(`[\pP]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["!" "."] // reg = regexp.MustCompile(`[\PP]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello " "123 Go"] // reg = regexp.MustCompile(`[\p{Han}]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // [" "] // reg = regexp.MustCompile(`[\P{Han}]+`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello " "!123 Go."] // Hello Go reg = regexp.MustCompile(`Hello|Go`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello" "Go"] // H , reg = regexp.MustCompile(`^H.*\s`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello !123 "] // H , ( ) reg = regexp.MustCompile(`(?U)^H.*\s`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello "] // hello ( ), Go reg = regexp.MustCompile(`(?i:^hello).*Go`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello !123 Go"] // Go. reg = regexp.MustCompile(`\QGo.\E`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Go."] // , ( ) reg = regexp.MustCompile(`(?U)^.* `) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello "] // , , reg = regexp.MustCompile(` [^ ]*$`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // [" Go."] // “ ” reg = regexp.MustCompile(`(?U)\b.+\b`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello" " !" "123" " " "Go"] // 1 4 , o reg = regexp.MustCompile(`[^ ]{1,4}o`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello" "Go"] // Hello Go reg = regexp.MustCompile(`(?:Hell|G)o`) fmt.Printf("%q
", reg.FindAllString(text, -1)) // ["Hello" "Go"] // Hello Go, Hellooo、Gooo reg = regexp.MustCompile(`(?PHell|G)o`) fmt.Printf("%q
", reg.ReplaceAllString(text, "${n}ooo")) // "Hellooo !123 Gooo." // Hello Go reg = regexp.MustCompile(`(Hello)(.*)(Go)`) fmt.Printf("%q
", reg.ReplaceAllString(text, "$3$2$1")) // "Go !123 Hello." // reg = regexp.MustCompile(`[\f\t
\r\v\123\x7F\x{10FFFF}\\\^\$\.\*\+\?\{\}\(\)\[\]\|]`) fmt.Printf("%q
", reg.ReplaceAllString("\f\t
\r\v\123\x7F\U0010FFFF\\^$.*+?{}()[]|", "-")) // "----------------------" } ------------------------------------------------------------

다음으로 전송:https://www.cnblogs.com/golove/p/3269099.html

좋은 웹페이지 즐겨찾기