Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # 正規表現のいろいろ
  2.  
  3. ## 1. 文字列が正規表現に完全一致するかどうかをしらべる
  4. ### Ruby
  5. ~~~
  6. /^\d$/ === 'ho123ge' #=> false
  7. ~~~
  8. ### C++11
  9. ~~~
  10. std::regex_match("ho123ge", std::regex("\\d")); //=> false
  11. ~~~
  12.  
  13. ## 2. 文字列が正規表現に部分一致するかどうかをしらべる
  14. ### Ruby
  15. ~~~
  16. /\d/ === 'ho123ge' #=> true
  17. ~~~
  18. ### C++11
  19. ~~~
  20. std::regex_search("ho123ge", std::regex("\\d")); //=> true
  21. ~~~
  22.  
  23. ## 3. 一致する部分を取り出す
  24. ### Ruby
  25. ~~~
  26. 'ho123ge'[/\d+/] #=> "123"
  27. ~~~
  28. ### C++11
  29. ~~~
  30. std::string s = "ho123ge";
  31. std::smatch m;
  32. std::regex_search(s, m, std::regex("\\d+"));
  33. std::cout << m.str(); //=> "123"
  34. ~~~
  35.  
  36. ## 4. 一致する部分を全部取り出す
  37. ### Ruby
  38. ~~~
  39. "foobar".scan(/../) #=> ["fo", "ob", "ar"]
  40. ~~~
  41. ### C++11
  42. ~~~
  43. std::string s = "ho12g45e";
  44. std::regex re("\\d+");
  45. for (std::sregex_iterator it(s.begin(), s.end(), re), end; it != end; ++it) {
  46. std::cout << (*it).str() << "\n"; //=> "12", "45"
  47. }
  48. ~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement