Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 正規表現のいろいろ
- ## 1. 文字列が正規表現に完全一致するかどうかをしらべる
- ### Ruby
- ~~~
- /^\d$/ === 'ho123ge' #=> false
- ~~~
- ### C++11
- ~~~
- std::regex_match("ho123ge", std::regex("\\d")); //=> false
- ~~~
- ## 2. 文字列が正規表現に部分一致するかどうかをしらべる
- ### Ruby
- ~~~
- /\d/ === 'ho123ge' #=> true
- ~~~
- ### C++11
- ~~~
- std::regex_search("ho123ge", std::regex("\\d")); //=> true
- ~~~
- ## 3. 一致する部分を取り出す
- ### Ruby
- ~~~
- 'ho123ge'[/\d+/] #=> "123"
- ~~~
- ### C++11
- ~~~
- std::string s = "ho123ge";
- std::smatch m;
- std::regex_search(s, m, std::regex("\\d+"));
- std::cout << m.str(); //=> "123"
- ~~~
- ## 4. 一致する部分を全部取り出す
- ### Ruby
- ~~~
- "foobar".scan(/../) #=> ["fo", "ob", "ar"]
- ~~~
- ### C++11
- ~~~
- std::string s = "ho12g45e";
- std::regex re("\\d+");
- for (std::sregex_iterator it(s.begin(), s.end(), re), end; it != end; ++it) {
- std::cout << (*it).str() << "\n"; //=> "12", "45"
- }
- ~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement