程序语言中的正则表达式

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

程序语言中的正则表达式

java处理正则表达式

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

package getIP;

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;

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

    Map<String,Integer>result = <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">new</span> HashMap<String,Integer>();
    
    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>;
    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>);
    GetIp.getfromFile(result,f);
    
    GetIp.print(result);
}

<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>{
    <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">for</span>(String ip:result.keySet()){
        System.out.println(ip+<span class="hljs-string" style="color: #ec7600;">"--"</span>+result.get(ip));
    }
}

<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>{
    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>;
    Pattern pa = Pattern.compile(regex);
    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));
    String line = <span class="hljs-string" style="color: #ec7600;">""</span>;
    <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>){
        Matcher match = pa.matcher(line);
        <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">while</span>(match.find()){
            String x = match.group();
            <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">if</span>(result.containsKey(x)){
                result.put(x,result.get(x)+<span class="hljs-number" style="color: #ffcd22;">1</span>);
            }
            <span class="hljs-keyword" style="color: #93c763; font-weight: bold;">else</span>{
                result.put(x,<span class="hljs-number" style="color: #ffcd22;">1</span>);
            }
        }
    }
    br.close();
    
}
Map<String,<span class="hljs-number" style="color: #ffcd22;">1</span>); } } } br.close(); }}

python处理正则表达式

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

import re

author = 'hgfdo'

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

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

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

原文链接:https://www.f2er.com/regex/359947.html

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