Loading ...
Sorry, an error occurred while loading the content.

Str.replace_matched

Expand Messages
  • Erik de Castro Lopo
    Hi all, Does anyone have an example which uses Str.replace_matched? I ve read the docs, but I still can t make it do what I want. Cheers, Erik --
    Message 1 of 3 , Aug 31, 2004
    • 0 Attachment
      Hi all,


      Does anyone have an example which uses Str.replace_matched?
      I've read the docs, but I still can't make it do what I want.

      Cheers,
      Erik
      --
      +-----------------------------------------------------------+
      Erik de Castro Lopo nospam@... (Yes it's valid)
      +-----------------------------------------------------------+
      "You don't have make it your sole purpose in life, but could you at
      least sacrifice a rubber chicken upon the altar of literacy?"
      -- TackHead on Slashdot
    • Radu Grigore
      ... #load str.cma ;; open Str let s = baubau ;; let r = regexp (.* ) ;; string_match r s 0;; let result = replace_matched xxx 1 yyy s;; The value of
      Message 2 of 3 , Aug 31, 2004
      • 0 Attachment
        --- Erik de Castro Lopo <yahoo-erikd@...> wrote:

        > Does anyone have an example which uses Str.replace_matched?
        > I've read the docs, but I still can't make it do what I want.

        #load "str.cma";;
        open Str
        let s = "baubau";;
        let r = regexp "\\(.*\\)";;
        string_match r s 0;;
        let result = replace_matched "xxx \\1 yyy" s;;

        The value of result at the end is "xxx baubau yyy".

        regards,
        radu


        __________________________________________________
        Do You Yahoo!?
        Tired of spam? Yahoo! Mail has the best spam protection around
        http://mail.yahoo.com
      • William D. Neumann
        ... And a slightly more complex, though terribly contrived example: #load str.cma ;; let s = 1000 llamas destroyed Mr. Pettigore s petunias. let t = I
        Message 3 of 3 , Aug 31, 2004
        • 0 Attachment
          On Tue, 31 Aug 2004, Radu Grigore wrote:

          > #load "str.cma";;
          > open Str
          > let s = "baubau";;
          > let r = regexp "\\(.*\\)";;
          > string_match r s 0;;
          > let result = replace_matched "xxx \\1 yyy" s;;
          >
          > The value of result at the end is "xxx baubau yyy".

          And a slightly more complex, though terribly contrived example:

          #load "str.cma";;
          let s = "1000 llamas destroyed Mr. Pettigore's petunias."
          let t = "I heard it was 277 buffalo in Mrs. Ongala's tulips.";;
          let r = Str.regexp
          "[^0-9]*\\([0-9]+\\).*\\(Mr\\(s\\)*\\.\\)\\(.+\\)'s";;
          let just_the_facts s =
          if Str.string_match r s 0 then
          Str.replace_matched "Number: \\1, Title: \\2, Surname: \\4\n" s
          else
          "This guy knows nothing...\n";;

          # just_the_facts s;;
          - : string = "Number: 1000, Title: Mr., Surname: Pettigore\n"
          # just_the_facts t;;
          - : string = "Number: 277, Title: Mrs., Surname: Ongala\n"
          # just_the_facts "My mother said bunnies are mean!";;
          - : string = "This guy knows nothing...\n"

          William D. Neumann

          ---

          "Well I could be a genius, if I just put my mind to it.
          And I...I could do anything, if only I could get 'round to it.
          Oh we were brought up on the space-race, now they expect you to clean toilets.
          When you've seen how big the world is, how can you make do with this?
          If you want me, I'll be sleeping in - sleeping in throughout these glory days."

          -- Jarvis Cocker

          Think of XML as Lisp for COBOL programmers.

          -- Tony-A (some guy on /.)
        Your message has been successfully submitted and would be delivered to recipients shortly.