Java – reformat code using regular expressions

We have an ArrayList of items in several classes. Every time I want to insert a new item in the list, it will cause me trouble It was a mistake for me to design the course my way, but now changing the design will be more painful than its value (bureaucratic waterfall model.) I should expect the format to change. The customer provides us with waterfall files. Damn it

I want to write a simple script in Python, enter a class, add the item to the list, and then add all the retrieval of the following items This doesn't sound clear:

Foo extends Bar{
    public Foo(){
        m_Tags.add("Jane");
        m_Tags.add("Bob");
        m_Tags.add("Jim");
    }

    public String GetJane() { return m_ParsedValue.get( m_Tags.get(1) ); }
    public String GetBob() { return m_ParsedValue.get( m_Tags.get(2) ); }
    public String GetJim() { return m_ParsedValue.get( m_Tags.get(3) ); }
}

Do you think I want to add a value between "Jane" and "Bob", and then I have to add an integer in the get * function I just want to write a simple script in Python to complete my work Some people have great respect for the proposed regular expressions

Edit:

Yes, LinkedHashMap It's so simple, so simple, so it's not in the design specification now I hate waterfalls I hate it The whole process is a "small" and "simple" part, which should not take too much time to design I made a mistake It's in trouble now

Solution

You want regular expressions to be as flexible as the compiler in terms of spaces between tokens Doing so and mimicking the use of whitespace can make the pattern very confusing The following code (sorry: Perl, not Python) edits the source file in place

#! /usr/bin/perl -i.bak    
use warnings;
use strict;
my $template =
  '^( public
      String
      Get)(\w+)( \( \) { return
        m_ParsedValue . get \( m_Tags . get \( )(\d+)( \) \) ; } )$';
$template =~ s/ +/\\s*/g;
$template =~ s/(\r?\n)+/\\s+/g;
my $getter = qr/$template/x;

die "Usage: $0 after new-name source ..\n" unless @ARGV >= 3;
my $after = shift;
my $add   = shift;
my $index;
while (<>) {
  unless (/$getter/) {
    print;
    next;
  }
  my($abc,$name,$lmno,$i,$xyz) = ($1,$2,$3,$4,$5);
  if (defined $index) {
    print join "" => $abc,++$index,$xyz;
  }
  else {
    if ($name eq $after) {
      $index = $i;
      print; print join "" => $abc,$add,$xyz;
    }
    else { print; }
  }
}

For example,

$./add-after Jane Foo code.java
$cat code.java
Foo extends Bar{
    public Foo(){
        m_Tags.add("Jane");
        m_Tags.add("Bob");
        m_Tags.add("Jim");
    }

    public String GetJane() { return m_ParsedValue.get( m_Tags.get(1) ); }
    public String GetFoo() { return m_ParsedValue.get( m_Tags.get(2) ); }
    public String GetBob() { return m_ParsedValue.get( m_Tags.get(3) ); }
    public String GetJim() { return m_ParsedValue.get( m_Tags.get(4) ); }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>