【问题标题】:Bash script reg-exp and objective-cBash 脚本 reg-exp 和 Objective-c
【发布时间】:2012-06-20 12:09:31
【问题描述】:

我是脚本语言的新手,我想为你们提供帮助

我想创建一个名为 constant.h 的文件,并在特定目录中的所有文件 *.m 中搜索 reg-exp @"LBL_[[0-9]]+" ex: @"LBL_75847",然后将这些匹配项写入 constant.h(如果有)不会像这样写入(不会复制到文件常量中)

NSString *const C_LBL_[[0-9]] = thematch; // exp NSString *const C_LBL_78787 = @"LBL_78787";

此外,此脚本还应将所有匹配的 @"LBL_[[0-9]]+" 替换为 *.m 文件,其常量 var 为 C_LBL_[[0-9]]+

提前致谢

【问题讨论】:

  • 你昨天不是问了几乎完全相同的问题吗? stackoverflow.com/questions/11105541/linux-command-line-regex
  • 不,实际上我想要一个找到匹配项并将其保存到像这样的文件的完整脚本 NSString *CLBL_26000 = @"LBL_26000";
  • 我有目标 c 类,其中包含很多 2000 年左右的字符串,例如 @"LBL_134343",实际上我想将所有这些字符串导出到一个文件中 constant.h 例如​​:NSString *CLBL_26000 = @"LBL_26000 " 并将类中的所有字符串替换为常量/var

标签: objective-c regex bash


【解决方案1】:

我找到了解决办法

$ cat ocstr.awk

# If the code contains @"LBL_....", replace it with C_LBL_... and
# remember the labels for later.
match($0, /@"LBL_[0-9]*\"/) {
        S=$0; P=""

        do        # Loop until there's nothing left in the right half
        {
                A[L=substr(S, RSTART+2, RLENGTH-3)]++; # Remember label
                P=P substr(S, 1, RSTART-1) "C_" L;
                S=substr(S, RSTART+RLENGTH);
        }
        while(match(S, /@"LBL_[0-9]*\"/)); # Keep hunting for more labels

        $0=P S; # Update the line to be printed
}

# If we are given file.pm, print into file.m
{       O=FILENAME;
        sub(/[.]pm$/, ".m", O);
        print > O;      }

END {
        for(L in A) printf("NSString *const C_%s = @\"%s\";\n", L, L);
}

$ awk -f ocstr.awk dir/*.pm > list

$ cat list

NSString *const C_LBL_10011 = @"LBL_10011";
NSString *const C_LBL_10012 = @"LBL_10012";
NSString *const C_LBL_10804 = @"LBL_10804";
NSString *const C_LBL_10000 = @"LBL_10000";

$ cat dir/*.m

tableviewlogin.backgroundView = nil;
    [welcometitle setFont:[UIFont fontWithName:FONTB size:FONTSIZETITLE]];
    [welcometitle setText:[MSharedFunctions UF8ErrorMessageForCode:C_LBL_10000]];
    [welcometitle setTextAlignment:UITextAlignmentCenter];
    NSString *buttontitle = [MSharedFunctions UF8ErrorMessageForCode:C_LBL_10012];
    [forgotpassword setTitle:buttontitle forState:UIControlStateNormal];
    [forgotpassword setTitle:buttontitle forState:UIControlStateHighlighted];
    [self.navigationItem setTitle:[MSharedFunctions UF8ErrorMessageForCode:C_LBL_10011]];
    [[self tabBarItem] setTitle:[MSharedFunctions UF8ErrorMessageForCode:C_LBL_10804]];

$

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多