是否有用于将前4个字符添加到字符串末尾和将后4个字符添加到字符串末尾的正则表达式?

时间:2019-04-05 04:14:01

标签: regex notepad++

我有一些行需要更改。它们是蛋白质序列。如何将行的前4个字符复制到行的末尾,同时又将后4个字符复制到行的开头? 字符串是使它复杂化的变量,例如:

  

> X

     

LTGLGIGTGMAATIINAISVGLSAATILSLISGVASGGAWVLAGAKQALKEGGKKAGIAF

     

> Y

     

LVATGMAAGVAKTIVNAVSAGMDIATALSLFSGAFTAAGGIMALIKKYAQKKLWKQLIAA

此外,我如何排除开头带有'>'的行(这些是相应序列的名称)?

有人知道正则表达式可以使它工作吗?

我已经尝试过一些正则表达式解决方案,但是我对这种事情不太有经验,我可以找到结尾字符串,但无法替换它:

查找:

  

(...)$

替换:

  

^ $ 2 $ 1“

我想要实现的一个例子是:

  

> 1

     

ABCDEFGHIJKLMNOPQRSTUVWXYZ

成为:

  

> 1

     

WXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCD

谢谢

3 个答案:

答案 0 :(得分:4)

尝试以正则表达式模式在以下模式下进行查找:

^([A-Z]{4}).*([A-Z]{4})$

然后替换为前四个和后四个字符:

$2$0$1

Demo

答案 1 :(得分:3)

您可以在下面使用正则表达式。

    var my_country = "United States"; // country which is going to auto selected

    var url_country="<?=base_url()?>country.json";

    var negara_list ="<option value=''></option>";

     $.getJSON(url_country, function(data) {

       $.each(data,function(key,val){

          if(my_country == val)
          {
              negara_list += "<option value="+val+" selected>"+val+"</option>";
          }
          else
          {
               negara_list += "<option value="+val+">"+val+"</option>"; 
           }

         });

       $(".negara").html(negara_list);
    });

https://regex101.com/r/W786uL/3上查看如何使用替换功能。

^(([A-Z]{4})([A-Z]*)([A-Z]{4}))$

^ asserts the position at the start of the line, so nothing can come before it.
( is the start of a capture group, this is group 1.
( is the start of a capture group, this is group 2. This group is inside group 1.
[A-Z]{4} means exactly 4 capital characters from A to Z.
) is the end of capture group 2.
( is the start of a capture group, this is group 3.
[A-Z]* matches capital characters from A to Z between zero and infinite times.
) is the end of capture group 3.
( is the start of a capture group, this is group 4.
[A-Z]{4} means exactly 4 capital characters from A to Z.
) is the end of capture group 4.
$ asserts the position at the end of the line, so nothing can come after it.

答案 2 :(得分:0)

您可以使用

^(.{4})(.*?)(.{4})$
  • ^-刺痛开始
  • (.{4})-匹配除换行符以外的所有字符
  • (.*?)-匹配任何字符零个或多个时间(惰性模式)
  • $-字符串结尾

Demo