程序语言中的正则表达式

前端之家收集整理的这篇文章主要介绍了程序语言中的正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

程序语言中的正则表达式

java处理正则表达式

读取文件,并解析文件中的ip地址。例子:

  1. package getIP;
  2. import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern;

  3. class GetIp{ public static void main(String[] args) throws IOException {

  4.     Map<String,Integer>result = <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">new</span> HashMap<String,Integer>();
  5.     
  6.     String s = <span class="hljs-string" style="color: #ec7600;">"Aug 23 09:29:46 oracle205 sshd[222466]: Accepted password for root from 124.127.207.154 port 46955 ssh2"</span>;
  7.     File f = <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">new</span> File(<span class="hljs-string" style="color: #ec7600;">"E:\\secure-20150830"</span>);
  8.     GetIp.getfromFile(result,f);
  9.     
  10.     GetIp.print(result);
  11. }
  12. <span class="hljs-function"><span class="hljs-keyword" style="color: #93c763; font-weight: bold;">public</span> <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">static</span> <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">void</span> <span class="hljs-title" style="font-weight: bold;">print</span><span class="hljs-params">(Map<String,Integer>result)</span></span>{
  13.     <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">for</span>(String ip:result.keySet()){
  14.         System.out.println(ip+<span class="hljs-string" style="color: #ec7600;">"--"</span>+result.get(ip));
  15.     }
  16. }
  17. <span class="hljs-function"><span class="hljs-keyword" style="color: #93c763; font-weight: bold;">public</span> <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">static</span> <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">void</span> <span class="hljs-title" style="font-weight: bold;">getfromFile</span><span class="hljs-params">(Map<String,Integer>result,File f)</span> <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">throws</span> IOException</span>{
  18.     String regex = <span class="hljs-string" style="color: #ec7600;">"((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"</span>;
  19.     Pattern pa = Pattern.compile(regex);
  20.     BufferedReader br = <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">new</span> BufferedReader(<span class="hljs-keyword" style="color: #93c763; font-weight: bold;">new</span> FileReader(f));
  21.     String line = <span class="hljs-string" style="color: #ec7600;">""</span>;
  22.     <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">while</span>((line = br.readLine())!=<span class="hljs-keyword" style="color: #93c763; font-weight: bold;">null</span>){
  23.         Matcher match = pa.matcher(line);
  24.         <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">while</span>(match.find()){
  25.             String x = match.group();
  26.             <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">if</span>(result.containsKey(x)){
  27.                 result.put(x,result.get(x)+<span class="hljs-number" style="color: #ffcd22;">1</span>);
  28.             }
  29.             <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">else</span>{
  30.                 result.put(x,<span class="hljs-number" style="color: #ffcd22;">1</span>);
  31.             }
  32.         }
  33.     }
  34.     br.close();
  35.     
  36. }
  37. Map<String,<span class="hljs-number" style="color: #ffcd22;">1</span>);
  38. }
  39. }
  40. }
  41. br.close();
  42. }}

python处理正则表达式

读取文件,并解析文件中的ip地址。

  1. import re
  2. author = 'hgfdo'

  3. f = open(r"e:\secure",'r')

  4. pattern = re.compile(r'(\d+).(\d+).(\d+).(\d+)') result = {}

  5. for line in f.readlines(): match = pattern.search(line) if match: x = match.group() if x in result.keys(): result[x] = result[x] + 1 else: result[x] = 1

    for x in result.keys(): print "%s --- %d" %(x,result[x])

注意:在java中使用的是Pattern.matcher(String),在String中搜索匹配的字符串(不管这个匹配是不是在字符串的起始位置),而python的match(string)只能匹配出在字符串起始位置的子字符串,若子字符串在字符串中间,则匹配不出,需要使用search(String)来匹配字符串中的子字符串。


贺广福(heguangfu)@2015-9-17

猜你在找的正则表达式相关文章