jiro4989
11/12/2017 - 9:05 AM

Go言語正規表現テスト

Go言語正規表現テスト

#!/bin/bash
# -*- coding: utf-8 -*-

set -eux

echo "############################################################"
echo "文字列の有無の確認"
echo "############################################################"

echo "------------------------------------------------------------"
echo "go"
echo "------------------------------------------------------------"
go run ./match_string.go
echo "------------------------------------------------------------"
echo "python"
echo "------------------------------------------------------------"
python3 ./match_string.py

echo "############################################################"
echo "複数の文字列を取り出す+要素数を数える"
echo "############################################################"

echo "------------------------------------------------------------"
echo "go"
echo "------------------------------------------------------------"
go run ./find_all_string.go
echo "------------------------------------------------------------"
echo "python"
echo "------------------------------------------------------------"
python3 ./find_all_string.py

echo "############################################################"
echo "文字列を正規表現で置換する"
echo "############################################################"

echo "------------------------------------------------------------"
echo "go"
echo "------------------------------------------------------------"
go run ./replace_all_string.go
echo "------------------------------------------------------------"
echo "python"
echo "------------------------------------------------------------"
python3 ./replace_all_string.py

echo "############################################################"
echo "後方参照"
echo "############################################################"

echo "------------------------------------------------------------"
echo "go"
echo "------------------------------------------------------------"
go run ./backref.go
echo "------------------------------------------------------------"
echo "python"
echo "------------------------------------------------------------"
python3 ./backref.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

def main():
    p = re.compile(r"[0-4]\d")
    score = """
国語 : 43
数学 : 82
理科 : 92
社会 : 13
英語 : 21
    """
    print("置換前")
    print(score)
    print("置換後")
    print(re.sub(p, "70", score))

if __name__ == '__main__':
    main()


package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`[0-4]\d`)

	fmt.Println(`test ReplaceAllString <[0-4]\d>`)
	score := `
国語 : 43
数学 : 82
理科 : 92
社会 : 13
英語 : 21
`
	fmt.Println("置換前")
	fmt.Println(score)
	fmt.Println("置換後")
	fmt.Println(re.ReplaceAllString(score, "70"))
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

def main():
    p = re.compile(r"^https?://[^/]*/$")
    li = [
		"https://google.com/",
		"https://example.com/index.php",
		"C:\\workspace\\test\\go\\",
            ]
    for l in li:
        result = p.match(l) != None
        print("{:5s} : {}".format(str(result), l))

if __name__ == '__main__':
    main()


package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^https?://[^/]*/$`)

	fmt.Println("test Match : <^https?://[^/]*/$>")
	li := []string{
		"https://google.com/",
		"https://example.com/index.php",
		`C:\workspace\test\go\`,
	}
	for _, v := range li {
		fmt.Printf("%5v : %v\n", re.MatchString(v), v)
	}
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

def main():
    p = re.compile(r"2000/01/\d{2}")
    text = """
1999/12/31 23:42:28 テスト
2000/01/01 00:00:00 サンプル
2000/01/01 13:01:20 Example
2000/01/01 13:50:09 ほげさんぷる
2000/01/02 17:00:03 foobar
2000/01/02 17:00:04 hogefuga
2000/01/02 17:00:05 hogepiyo
    """
    print(text)

    matched = p.findall(text)
    print("結果")
    print("見つけた要素 : ", matched)
    print("見つけた数 : ", len(matched))

if __name__ == '__main__':
    main()


package main

import (
	"fmt"
	"regexp"
)

func main() {
	fmt.Println(`test FindAllString : <2000/01/\d{2}>`)

	re := regexp.MustCompile(`2000/01/\d{2}`)
	fmt.Println("検証用サンプルテキスト")
	text := `
1999/12/31 23:42:28 テスト
2000/01/01 00:00:00 サンプル
2000/01/01 13:01:20 Example
2000/01/01 13:50:09 ほげさんぷる
2000/01/02 17:00:03 foobar
2000/01/02 17:00:04 hogefuga
2000/01/02 17:00:05 hogepiyo
`
	fmt.Println(text)

	matched := re.FindAllString(text, -1)
	fmt.Println("結果")
	fmt.Println("見つけた要素 : ", matched)
	fmt.Println("見つけた数 : ", len(matched))
}
############################################################
文字列の有無の確認
############################################################
------------------------------------------------------------
go
------------------------------------------------------------
test Match : <^https?://[^/]*/$>
 true : https://google.com/
false : https://example.com/index.php
false : C:\workspace\test\go\
------------------------------------------------------------
python
------------------------------------------------------------
True  : https://google.com/
False : https://example.com/index.php
False : C:\workspace\test\go\
############################################################
複数の文字列を取り出す+要素数を数える
############################################################
------------------------------------------------------------
go
------------------------------------------------------------
test FindAllString : <2000/01/\d{2}>
検証用サンプルテキスト

1999/12/31 23:42:28 テスト
2000/01/01 00:00:00 サンプル
2000/01/01 13:01:20 Example
2000/01/01 13:50:09 ほげさんぷる
2000/01/02 17:00:03 foobar
2000/01/02 17:00:04 hogefuga
2000/01/02 17:00:05 hogepiyo

結果
見つけた要素 :  [2000/01/01 2000/01/01 2000/01/01 2000/01/02 2000/01/02 2000/01/02]
見つけた数 :  6
------------------------------------------------------------
python
------------------------------------------------------------

1999/12/31 23:42:28 テスト
2000/01/01 00:00:00 サンプル
2000/01/01 13:01:20 Example
2000/01/01 13:50:09 ほげさんぷる
2000/01/02 17:00:03 foobar
2000/01/02 17:00:04 hogefuga
2000/01/02 17:00:05 hogepiyo
    
結果
見つけた要素 :  ['2000/01/01', '2000/01/01', '2000/01/01', '2000/01/02', '2000/01/02', '2000/01/02']
見つけた数 :  6
############################################################
文字列を正規表現で置換する
############################################################
------------------------------------------------------------
go
------------------------------------------------------------
test ReplaceAllString <[0-4]\d>
置換前

国語 : 43
数学 : 82
理科 : 92
社会 : 13
英語 : 21

置換後

国語 : 70
数学 : 82
理科 : 92
社会 : 70
英語 : 70

------------------------------------------------------------
python
------------------------------------------------------------
置換前

国語 : 43
数学 : 82
理科 : 92
社会 : 13
英語 : 21
    
置換後

国語 : 70
数学 : 82
理科 : 92
社会 : 70
英語 : 70
    
############################################################
後方参照
############################################################
------------------------------------------------------------
go
------------------------------------------------------------
 true : 田中 太郎 : 太郎 田中
 true : 山本 次郎 : 次郎 山本
false : ほげ山 三郎 : ほげ山 三郎
 true : ばー ふー : ふー ばー
 true : hoge	piyo : piyo hoge
------------------------------------------------------------
python
------------------------------------------------------------
True  : 田中 太郎 : 太郎 田中
True  : 山本 次郎 : 次郎 山本
True  : ほげ山 三郎 : 三郎 ほげ山
True  : ばー ふー : ふー ばー
True  : hoge	piyo : piyo hoge
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

def main():
    p = re.compile(r"^([^\s]+)\s([^\s]+)$")
    names = [
        "田中 太郎",
        "山本 次郎",
        "ほげ山 三郎",
        "ばー ふー",
        "hoge	piyo",
            ]
    for name in names:
        match = p.match(name) != None
        rep = re.sub(p, "\\2 \\1", name)
        print("{:5s} : {} : {}".format(str(match), name, rep))

if __name__ == '__main__':
    main()


package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^([^\s]+)\s([^\s]+)$`)
	names := []string{
		"田中 太郎",
		"山本 次郎",
		"ほげ山 三郎",
		"ばー ふー",
		"hoge	piyo",
	}
	for _, v := range names {
		m := re.MatchString(v)
		rep := re.ReplaceAllString(v, "$2 $1")
		fmt.Printf("%5v : %s : %s\n", m, v, rep)
	}
}