在Groovy语言中如何根据字符串

时间:2016-04-05 14:44:21

标签: string groovy soapui

我希望修改字符串的值如果字符串中的某些位置是特定值,例如我有一个邮政编码L65 OBH,我需要执行以下操作:

(1)

如果字符串第一部分中的第一个值(由空格分割)= L则需要将其更改为T.这将给出:

T65 OBH

(2)

然后,如果字符串第一部分中的第二个值(由空格分割)= 6,则需要将其更改为7.这将给出:

T75 OBH

(3)

然后,如果字符串第二部分中的第一个值(由空格分割)= O,则需要将其更改为2.这将给出:

T75 2BH

(4)

然后,如果字符串第二部分中的第3个值(由空格分割)= H,则需要将其更改为P.这将给出:

T75 2BP

我假设我需要使用replaceall和一些IF语句,但我正在努力解决这个问题,特别是如何将邮政编码的两个不同部分分开,将它们视为单独的enteties ....有人请帮忙

2 个答案:

答案 0 :(得分:1)

我会为替换规则编写辅助方法:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    ## add ssl entries when https has been set in config
    ssl_certificate      certs\myCert.cert;
    ssl_certificate_key  certs\myCert.cert;
    ssl_session_cache shared:SSL:1m;
    ssl_prefer_server_ciphers   on;
    ## server configuration
    server {
        listen 443 ssl;
        listen 9091 ;

        server_name localhost;
        if ($http_x_forwarded_proto = '') {
            set $http_x_forwarded_proto  $scheme;
        }
        ## Application specific logs
        ## access_log /var/log/nginx/localhost-access.log timing;
        ## error_log /var/log/nginx/localhost-error.log;
        rewrite ^/$ /artifactory/webapp/ redirect;
        rewrite ^/artifactory/?(/webapp)?$ /artifactory/webapp/ redirect;
        location /artifactory/ {
        proxy_read_timeout  900;
        proxy_pass_header   Server;
        proxy_cookie_path ~*^/.* /;
        proxy_pass         http://localhost:8080/artifactory/;
        proxy_set_header   X-Artifactory-Override-Base-Url $http_x_forwarded_proto://$host:$server_port/artifactory;
        proxy_set_header    X-Forwarded-Port  $server_port;
        proxy_set_header    X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_set_header    Host              $http_host;
        proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
        }
    }

    ## server configuration
    server {
        listen 4441 ssl;


        server_name localhost;
        if ($http_x_forwarded_proto = '') {
            set $http_x_forwarded_proto  $scheme;
        }
        ## Application specific logs
        ## access_log /var/log/nginx/localhost-access.log timing;
        ## error_log /var/log/nginx/localhost-error.log;
        rewrite ^/(v1|v2)/(.*) /artifactory/api/docker/docker-local-v1/$1/$2;
        client_max_body_size 0;
        chunked_transfer_encoding on;
        location /artifactory/ {
        proxy_read_timeout  900;
        proxy_pass_header   Server;
        proxy_cookie_path ~*^/.* /;
        proxy_pass         http://localhost:8080/artifactory/;
        proxy_set_header   X-Artifactory-Override-Base-Url $http_x_forwarded_proto://$host:$server_port/artifactory;
        proxy_set_header    X-Forwarded-Port  $server_port;
        proxy_set_header    X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_set_header    Host              $http_host;
        proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
        }
    }

    ## server configuration
    server {
        listen 4442 ssl;


        server_name localhost;
        if ($http_x_forwarded_proto = '') {
            set $http_x_forwarded_proto  $scheme;
        }
        ## Application specific logs
        ## access_log /var/log/nginx/localhost-access.log timing;
        ## error_log /var/log/nginx/localhost-error.log;
        rewrite ^/(v1|v2)/(.*) /artifactory/api/docker/docker-local-v2/$1/$2;
        client_max_body_size 0;
        chunked_transfer_encoding on;
        location /artifactory/ {
        proxy_read_timeout  900;
        proxy_pass_header   Server;
        proxy_cookie_path ~*^/.* /;
        proxy_pass         http://localhost:8080/artifactory/;
        proxy_set_header   X-Artifactory-Override-Base-Url $http_x_forwarded_proto://$host:$server_port/artifactory;
        proxy_set_header    X-Forwarded-Port  $server_port;
        proxy_set_header    X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_set_header    Host              $http_host;
        proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
        }
    }
}

答案 1 :(得分:0)

def strVal= "L65 OBH"
strVal.replaceFirst(/^L/, "T")

def strVal1= "L65 OBH"
strVal1.replaceFirst(/^6/, "7")

等使用相同的replaceFirst()方法