格式化字符串以查找和替换段

时间:2016-01-02 17:15:35

标签: java string

我在Java中有以下字符串。

$('.ro').datepicker({
  dateFormat: 'yy-mm-dd',
  disabled:true
});




$('#dob, table.grid > tbody> tr > td > input[type="text"]').datepicker({
  dateFormat: 'yy-mm-dd',
  changeMonth: true,
  changeYear: true,
  yearRange: '1900:2016'
    });

//$('#dob, table.grid > tbody> tr > td > input[type="text"]').change(function(){
   //updateTable($(this));
//});

$('body').on('change','#dob, table.grid > tbody> tr > td > input[type="text"]',function(){
   updateTable($(this));
});

function updateTable(select){
          if(select.attr("id") == "dob"){
            $('.ro').val('');
            var rowLen = $('table.grid > tbody >tr').length;
          }else{
            var rowLen=1;
          }

           var r = 0;
           var pr = 0;
           while(r<rowLen){
              if(select.attr("id") == "dob"){
                 var thisRow = $('table.grid > tbody > tr').eq(r+1);
              }else{
                 var thisRow = select.closest('tr');
              }


           var inputlen = thisRow.find('input[type="text"]').length;
           var inputIndex = thisRow.find(':input[type="text"]').index(this);
           var RowInput = thisRow.find('input[type="text"]');
           var RowSch =  thisRow.prev().find('input[type="text"]');
           var thisDate = select.datepicker('getDate');
           var vname = thisRow.prev().find('td:eq(0)').text();
           var pros = false;
           var thisCell = select.closest('td').index();

           for($i=inputIndex;$i<(inputlen-1);$i++){
             var c = RowSch.eq($i+1).attr('c');
             var d = parseInt(RowSch.eq($i+1).attr('d'),10);
             var a = parseInt(RowSch.eq($i+1).attr('a'),10);
             if(a==0){
               if($('#dob').datepicker('getDate')==null){
                 alert('Date of Birth is required for Schedule Date of Dose-'+($i+2)+' of '+vname+' !!');
                 break;
               }else{
                 thisDate=$('#dob').datepicker('getDate');
                 //alert('Datepicker date: '+thisDate);
                 pros=true;
               }
             }else if(a==99){
               //break;
               pros=false;
             }else{
                if(RowInput.eq(a-1).datepicker('getDate')!=null){
                   thisDate = RowInput.eq(a-1).datepicker('getDate');
                   pros = true;
                }else if(RowSch.eq(a-1).datepicker('getDate')!=null){
                   thisDate = RowSch.eq(a-1).datepicker('getDate');
                   pros = true;
                }else{
                   alert('Date of Dose-'+a+' is required for Schedule Date of Dose-'+($i+2)+' of '+vname+' !!');
                   pros = false;
                   break;
                }
             }



             if(pros){

                if (c == 'y') {
                   thisDate.setFullYear(thisDate.getFullYear() + d);
                }
                if (c == 'd') {
                    thisDate.setDate(thisDate.getDate() + d);
                }
                if (c == 'm') {
                    thisDate.setMonth(thisDate.getMonth() + d);
                }
                //thisDate.setDate(thisDate.getDate() + 30);
                RowSch.eq($i+1).datepicker('setDate', thisDate);
             }
            pr = pr+1;
           }
           r=r+2;

           //alert(thisDate.setDate(thisDate.getDate() + 30));
          }
}

我想那个没有。然后是String abc="My name 233:23 is Shefali MNT+2:199999' MNT+12:40 xyzpqrst"; 然后是冒号(:),即上述情况下的199999应该替换为1,其余字符串应该保持不变。

即。 O / p应为MNT+2

第二个例如。 &GT;&GT; 如果输入字符串为"My name 233:23 is Shefali MNT+2:1' MNT+12:40 xyzpqrst"

O / P应为"ABC : MNT+232421:9' MNT+39191: hks"

我尝试过很多东西,但无法理解。

有人可以帮忙吗?

5 个答案:

答案 0 :(得分:2)

您可以使用以下正则表达式:(MNT\+\d+):\d+'并使用此替换字符串:$1:1'

查看实际操作:https://regex101.com/r/eF6nA9/1

答案 1 :(得分:0)

使用replaceAll()

String abc="My name 233:23 is Shefali MNT+2:199999' MNT+12:40 xyzpqrst";

abc = abc.replaceAll("\\d+'","1'");

System.out.print(abc); //My name 233:23 is Shefali MNT+2:1' MNT+12:40 xyzpqrst

答案 2 :(得分:0)

您可以使用一些分组表达式,例如

String[] arr = { "My name 233:23 is Shefali MNT+2:199999' MNT+12:40 xyzpqrst",
        "ABC : MNT+232421:9' MNT+39191: hks" };
String[] out = { "My name 233:23 is Shefali MNT+2:1' MNT+12:40 xyzpqrst",
        "ABC : MNT+232421:1' MNT+39191: hks" };
Pattern p = Pattern.compile("(.*):(\\d+)'(.*)");
for (int i = 0; i < arr.length; i++) {
    Matcher m = p.matcher(arr[i]);
    if (m.matches()) {
        String t = m.group(1) + ":1'" + m.group(3);
        System.out.printf("%s = %s %s%n", t, out[i], t.equals(out[i]));
    }
}

输出(根据要求,但为此帖格式化)

My name 233:23 is Shefali MNT+2:1' MNT+12:40 xyzpqrst = 
    My name 233:23 is Shefali MNT+2:1' MNT+12:40 xyzpqrst true
ABC : MNT+232421:1' MNT+39191: hks = 
    ABC : MNT+232421:1' MNT+39191: hks true

答案 3 :(得分:0)

使用正则表达式replaceAll()

String abc="My name 233:23 is Shefali MNT+2:199999' MNT+12:40 xyzpqrst";
abc.replaceAll("(MNT\\+\\d+):\\d+'", "$1:1'")

它将替换所有出现的给定模式。

答案 4 :(得分:0)

正则表达式解决方案是更简单/更清晰的解决方案但是为了进行比较,让我们看一下不同的方式。

String abc="My name 233:23 is Shefali MNT+2:199999' MNT+12:40 xyzpqrst";

//get index of the start of first "MNT"
int mntIndex = abc.indexOf("MNT+2:");

//get index of the second "MNT" location
int mntIndex2 = abc.indexOf("' MNT", mntIndex);

//now set abc equal to a substring up to the end of "MNT+2:"
//adding 6 (the length of "MNT+2:")
//manually adding "1" 
//finishing with a substring of abc starting 
//from the "' MNT" index up to the end of abc (abc.length())
abc = abc.substring(0, mntIndex) + 1 + abc.substring(mntIndex2,abc.length());