/tutorial/regular_expression/example1.e

http://github.com/tybor/Liberty · Specman e · 48 lines · 33 code · 4 blank · 11 comment · 2 complexity · 68e6ac2581642fab44f26be38fd0f945 MD5 · raw file

  1. class EXAMPLE1
  2. --
  3. -- How to do pattern matching with Liberty Eiffel:
  4. --
  5. -- compile -o example1 -boost example1
  6. --
  7. create {ANY}
  8. make
  9. feature {ANY}
  10. make
  11. local
  12. factory: REGULAR_EXPRESSION_BUILDER; number: REGULAR_EXPRESSION; tmp: STRING
  13. do
  14. factory.set_extended_legibility
  15. -- Create the regular expression from the pattern.
  16. number := factory.convert_perl_pattern("[
  17. [0-9]+ # one or more digits
  18. ]")
  19. check
  20. -- The given pattern is valid. Else, consult
  21. -- factory.last_error_message and factory.last_error_position
  22. number /= Void
  23. end
  24. io.put_string("Please write some text.%N")
  25. io.read_line
  26. -- Try if the pattern match some string
  27. if number.match(io.last_string) then
  28. io.put_string("There is a number in your text. Detected value is : ")
  29. tmp := ""
  30. -- Access to the matching text
  31. number.append_pattern_text(io.last_string, tmp)
  32. io.put_string(tmp)
  33. io.put_new_line
  34. -- Try to continue matching on the same string
  35. if number.match_next(io.last_string) then
  36. io.put_string("There is another number.%N")
  37. else
  38. io.put_string("There is no other number.%N")
  39. end
  40. else
  41. io.put_string("There is not any number in your text.%N")
  42. end
  43. end
  44. end -- class EXAMPLE1