SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Regex find/replace over several files...

    Hoping I might be able to get some help on writing a regular expression search/replace function that modifies a bunch of files. I've got the pseudocode, but need help with syntax. Here's an example showing the pattern:
    Changing this
    Code:
    Class my.foo.bar {
    
    }
    to this
    Code:
    Package my.foo{
    	Class bar {
    
    	}
    }
    Here is the pseudo code I came up with
    For each file:
    1) Find the line that starts with "class[space]"
    2) from that file, strip out everything after [space] to the last period on that line, store in myVar without the final period (in this case: my.foo)
    3) add tab before every line in file
    4) insert "Package[space][myVar]{" at beginning of file
    5) insert "}" at the very end of file
    6) save file and close

    It's my first crack at Ruby, so even if no one can provide me with actual code, any insight would really help.

    thanks in advance!

    roguerum. . .

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can convert a dir with this code:

    Code:
    def convert_code(a)
      name = a.match(/Class ([a-zA-Z.]+)/)[1]  # "foo.bar.baz"
      code = a.split("\n")[1..-2].join("\n  ") # the code inside { ... }
      dot = name.rindex('.')
      package, klass = name[0...dot], name[(dot+1)..-1] # "foo.bar", "baz"
    
      <<CODE
    Package #{package} {
      Class #{klass} {
      #{code}
      }
    }
    CODE
    end
    
    def convert_file(src)
      r = convert_code(File.open(src, 'r').read)
      File.open(src, 'w').write(r)
    end
    
    def convert_dir(d)
      for item in Dir[d + '/**']
        convert_file(item) if File.file?(item)
      end
    end
    
    convert_dir('testdir')
    If the code doesn't work, or if you need explanation, ask!
    Last edited by Fenrir2; Jun 26, 2006 at 14:25.

  3. #3
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A minor suggestion for Fen's regexps. It might be benificial to add a check for 1 or more spaces whenever you expect one. Most languages don't enforce having just one space inbetween keywords so for instance you could have 2 spaces inbetween the class declaration and the name of the class so adding \s+ would cover this case as well.

    Code:
    /Class\s+([a-zA-Z.]+)/
    You never know when an extra space character could get inserted when typing something out and it won't be reported as a syntax error because its not.
    Erh

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A good suggestion

    Do you have more suggestions? I don't like the first method at all, but I couldn't think of a better way.

  5. #5
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    A good suggestion

    Do you have more suggestions? I don't like the first method at all, but I couldn't think of a better way.


    Unfortunately no. I just noticed the regular expression because it's something thats bitten me quite often just trying to parse HTML on occasion. I'm still a beginner with Ruby so it looks good to me.
    Erh


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •