python regex note


  To remember python regex easily, we organise python regex notes from concrete to abstract, and from simple to sophisticated.

I, Important character:

  1, Quantitive character:

    ? :[0,1],   *:[0,infi),  +:(0,infi),  {n}:[n rep],  {m,}:[m, infi),  {,n}:[0,n],  {m,n}:[m,n],  

    ? (nogreedy option)

   2, Begin and End:

     ^: beginning,   $: ending

  3, Making own character class:

    [abc]  ,[^abc]: ^ rep non
  4, Character classes:

     \d,\D,\s,\S,\w,\W
  5: dot (or wildcard) character:

    . ,   .*,  (.*) ? : rep nongreedy fashion

II, Usefull regex arguments

  1, re.DOTALL : match all characters, including the newline character.

  2, re.I (re.IGNORECASE): ignore uppercase 

  3, re.VERBOSE : spread the regex over multiple lines with comments.

  4, | : we could use pipe character to cobine all three above arguments.

III, generate regex procedure:

  1, regex = re.compile(r"")

  2.1.1, regex.search("")

  2.1.2, regex.search("").group(), (or regex.search("").group(No.)),  greedy defalut

  2.2, regex.findall("")

  2.3 regex.sub("")

  3 , re.compile(r'''( )''', re.VERBOSE) : to manage more complex regrexes.

posted on 2016-05-08 17:58  xuezoushi  阅读(251)  评论(0编辑  收藏  举报

导航