如何创建正则表达式以删除字符串开头的所有`" 0"`?

时间:2015-12-10 05:33:45

标签: java regex

如何删除字符串开头的所有"0"

00011 -> 11
00123 -> 123
000101 -> 101
101 -> 101
000002500 -> 2500

我试过了:

            Pattern pattern = Pattern.compile("([1-9]{1}[0-9]?+)");
            Matcher matcher = pattern.matcher("00049");
            matcher.matches();
            whatYouNeed = matcher.group();

我有错误:No match found

4 个答案:

答案 0 :(得分:3)

我试试

System.out.println("Status: " + "00012010003".replaceAll("^0+", ""));   

或仅限正则表达式:

yourString.replaceAll("^0+", "");

其中

^ - matches only at start of string
0 - matches literal zeroes
+ - matches consecutive zeroes (at least one)

答案 1 :(得分:2)

您应该将replaceAll^0*一起使用,并替换为empty string,而不是找到匹配。

答案 2 :(得分:2)

如果您的String仅包含问题中所述的数字。您可以使用String.valueOf(Integer.parseInt("00011"))

答案 3 :(得分:0)

您必须使用正则表达式replaceFirst()String val = "000011100"; String newVal = val.replaceFirst("(?<=^)0+", ""); System.out.println(newVal); 进行此操作 但是如果它是另一种形式,则在regex之前将你的值解析为string。

11100

输出

?<=^

其中 0背后隐藏着。正则表达式模式仅匹配^ <script> function Validate(strDecision) { //alert("Validate"); debugger; if (strDecision == "EDIT") { if ($("#MerchDispName").val() == "" || $("#MerchDispName").val() == null) { $("#DivInfoNErrorMsg").addClass("alert-danger"); $("#DivInfoNErrorMsg").html("<span>There is no data for editing.</span>"); $("#DivInfoNErrorMsg").show(); return false; } else { debugger; $(".ReadOnly").removeAttr("readOnly", "readOnly"); $(".Disabled").removeAttr("disabled", "disabled"); $("#Update").show(); $("#Edit").hide(); GetDetails(); } } else { var bFlag = false; var iLengthPaymentMode = '@Model.dicPaymentModeVal.Count'; var iLengthProgramType = '@Model.dicProgramTypeVal.Count'; var i = 1; for (; i <= iLengthPaymentMode; ) { if ($("#PaymentCB" + i).is(":checked")) { $("#PaymentCB" + i + "hid").val($("#PaymentCB" + i).val()); bFlag = true; } i++; } if (bFlag == false) { $("#PaymentCBmsg").html("<span style='color:Red'>*Please select at least one payment option.</span>"); ShowErrorMsg("Please select at least one payment option."); $("#PaymentCBmsg").show(); return false; } for (; i <= iLengthProgramType; ) { if ($("#ProgramCB" + i).is(":checked")) { $("#ProgramCB" + i + "hid").val($("#ProgramCB" + i).val()); bFlag = true; } i++; } if (bFlag == false) { $("#ProgramCBmsg").html("<span style='color:Red'>*Please select at least one program type.</span>"); ShowErrorMsg("Please select at least one program type."); $("#ProgramCBmsg").show(); return false; } else { $(".Disabled").attr("disabled", "disabled"); $(".ReadOnly").attr("readOnly", "readOnly"); $("#Edit").show(); $("#Update").hide(); $("#frmMerchantEDIT").submit(); } } } $(document).ready(function () { //debugger; var bFlagErrorMessage = '@Model.FlagErrorMessage'; var bFlagInfoMessage = '@Model.FlagInfoMessage'; $('#CountryList').on('change', function () { debugger; var id = this.value; //$("#CountryID").val('0'); $("#CountryID").val(id); // or $(this).val() }); $('#StateList').on('change', function () { debugger; var id = this.value; //$("#StatesID").val('0'); $("#StatesID").val(id); alert(id); // or $(this).val() }); $('#CityList').on('change', function () { debugger; var id = this.value; //$("#CityID").val('0'); $("#CityID").val(id); //alert(this.value); // or $(this).val() }); if (bFlagErrorMessage == 'True') { debugger; var sErrorMsg = '@Model.ErrorMessage'; $("#DivInfoNErrorMsg").addClass("alert-danger"); $("#DivInfoNErrorMsg").html("<span>" + sErrorMsg + "</span>"); $("#DivInfoNErrorMsg").show(); $(".ReadOnly").removeAttr("readOnly", "readOnly"); $(".Disabled").removeAttr("disabled", "disabled"); $("#Update").show(); $("#Edit").hide(); } if (bFlagInfoMessage == 'True') { debugger; var sErrorMsg = '@Model.InfoMessage'; $("#DivInfoNErrorMsg").addClass("alert-success"); $("#DivInfoNErrorMsg").html("<span>" + sErrorMsg + "</span>"); $("#DivInfoNErrorMsg").show(); } }); </script> @using (Html.BeginForm("EDIT_MERCHANT", "MERCHANT_Permission", FormMethod.Post, new { @id = "frmMerchantEDIT", @class = "form-horizontal", @Role = "form" })) { @Html.AntiForgeryToken() //@Html.ValidationSummary() <div class="logmain_mechant"> <fieldset> <legend class="panel-heading panel-primary" style="font-size:140%">Edit Merchant</legend> <div id="DivInfoNErrorMsg" class="alert" hidden="hidden"></div> <div class="wrapper"> <div class="panel-group" id="accordion"> <div class="panel panel-primary"> <div class="panel-heading"> <a class="accordion-toggle" style="text-decoration:none" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"> <h2 class="panel-title" style="color:White;font-size:135%"> Personal <span class="indicator glyphicon glyphicon-chevron-down pull-right"></span> </h2> </a> </div> <div id="collapseOne" class="panel-collapse collapse in"> <div class="panel-body"> <div class="form-group"> <label for="MerchantName" class="control-label col-xs-8 col-sm-6 col-md-4">Merchant Name<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.MerchantName, new { @id = "MerchantName", @class = "form-control ReadOnly",@onclick= "ErrorDivHide()" }) @Html.ValidationMessageFor(m => m.MerchantName) </div> </div> <div class="form-group"> <label for="MerchDispName" class="control-label col-xs-8 col-sm-6 col-md-4">Merchant Display Name<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.MerchantDisplayName, new { @id = "MerchDispName", @class = "form-control", @onclick="hideme()",@placeholder="Merchant Display Name" }) @Html.ValidationMessageFor(m => m.MerchantDisplayName) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">Address1<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.Address1, new { @id = "Address1", @class = "form-control ReadOnly", @onclick = "ErrorDivHide()" }) @Html.ValidationMessageFor(m => m.Address1) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col- md-4">Address2:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.Address2, new { @id = "Address2", @class = "form-control ReadOnly", @onclick = "ErrorDivHide()" }) @Html.ValidationMessageFor(m => m.Address2) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">Address3:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.Address3, new { @id = "Address3", @class = "form-control ReadOnly", @onclick = "ErrorDivHide()" }) @Html.ValidationMessageFor(m => m.Address3) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">Country<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.DropDownListFor(m => m.CountryID, Model.liCountry, new { @id = "CountryList", @class = "form-control Disabled", @onchange = "GetStates()", @onclick = "ErrorDivHide()" }) @Html.HiddenFor(m => m.CountryID, new { @id="CountryId" }) @*@Html.TextBoxFor(m => m.CountryName, new { @id = "CountryName", @calss = "form-control" })*@ @Html.ValidationMessageFor(m => m.CountryID) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">State<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.DropDownListFor(m => m.StateID, Model.liState, new { @id = "StateList", @class = "form-control Disabled", @onchange = "GetCitis()", @onclick = "ErrorDivHide()" }) @Html.HiddenFor(m => m.StateID, new { @id = "StateIds" }) @*@Html.TextBoxFor(m => m.CountryName, new { @id = "CountryName", @calss = "form-control" })*@ @Html.ValidationMessageFor(m => m.StateName) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">City<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.DropDownListFor(m => m.CityID, Model.liCity, new { @id = "CityList", @class = "form-control Disabled", @onclick = "ErrorDivHide()" }) @Html.HiddenFor(m => m.CityID, new { @id = "CityIds" }) @*@Html.TextBoxFor(m => m.CountryName, new { @id = "CountryName", @calss = "form-control" })*@ @Html.ValidationMessageFor(m => m.CityName) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">ZipCode<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.ZipCode, new { @id = "ZipCode", @class = "form-control ReadOnly", @onclick = "ErrorDivHide()", @onkeypress = "return validateZipCode(event);" }) @Html.ValidationMessageFor(m => m.ZipCode) </div> </div> <div class="form-group"> <label class="control-label col-xs-10 col-sm-6 col-md-4">PG Merchant Name</label> <div class="col-xs-12 col-sm-10 col-md-8"> @*type="text" name="PayControllerMerName" id="PayControllerMerName" class="form-control" />*@ @Html.TextBoxFor(m => m.sPGMerchantName, new { @id = "PayControllerMerName", @class = "form-control ReadOnly", @onclick = "ErrorDivHide()" }) @Html.ValidationMessageFor(m => m.sPGMerchantName) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">PG Merchant ID<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.PayControllerMerID, new { @name = "PayControllerMerID", @id = "PayControllerMerID", @class = "form-control disabled" }) @Html.ValidationMessageFor(m => m.PayControllerMerID) </div> </div> @*<div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">Submit</button> </div> </div>*@ </div> </div> </div> <div class="panel panel-primary"> <div class="panel-heading"> <a class="accordion-toggle" data-toggle="collapse" style="text-decoration:none" data-parent="#accordion" href="#collapseTwo"> <h2 class="panel-title" style="color:White;font-size:135%"> Configuration <span class="indicator glyphicon glyphicon-chevron-down pull-right"></span> </h2> </a> </div> <div id="collapseTwo" class="panel-collapse collapse"> <div class="panel-body"> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">Merchant Website<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.ReturnURL, new { @id = "ReturnURL", @class = "form-control ReadOnly", @onclick = "ErrorDivHide()" }) @Html.ValidationMessageFor(m => m.ReturnURL) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">Merchant EmailID<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @Html.TextBoxFor(m => m.EmailID, new { @id = "EmailID", @class = "form-control ReadOnly", @onclick = "ErrorDivHide()" }) @Html.ValidationMessageFor(m => m.EmailID) </div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">Program Type<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @{ var i = 1; foreach (KeyValuePair<int, string> k in Model.dicProductType) { var ProductName = "ProgramCB" + i; <span class="col-sm-offset-1 pull-left"> <input type="checkbox" name="ProgramCB" value="@k.Key" checked="@Model.dicProgramTypeVal[k.Key]" id="@ProductName" class="Disabled" onclick="ProgramClick(@ProductName)" /><label for="@ProductName" style="font-weight:normal;">@k.Value</label> </span> i = i + 1; ProductName = ProductName + "hid"; <input type="hidden" id="@ProductName" name="ProgramCB" /> } } </div> <div hidden="hidden" id="ProgramCBmsg"></div> </div> <div class="form-group"> <label class="control-label col-xs-8 col-sm-6 col-md-4">Payment Mode<span style="color:Red">*</span>:</label> <div class="col-xs-12 col-sm-10 col-md-8"> @{ i = 1; foreach (KeyValuePair<int, string> k in Model.dicPaymentMode) { var elementName = "PaymentCB" + i; <input type="checkbox" name="PaymentCB" value="@k.Key" class="Disabled" checked="@Model.dicPaymentModeVal[k.Key]" id="@elementName" onclick="PaymentClick(@elementName)" /> <label for="@elementName" style="font-weight:lighter;font-size:normal">@k.Value</label> i = i + 1; elementName = elementName + "hid"; <input type="hidden" id="@elementName" name="PaymentCB" /> } } </div> @Html.HiddenFor(m => m.CurrentStateID, new { @id="StatesID"}) @Html.HiddenFor(m => m.CurrentCountryID, new { @id = "CountryID" }) @Html.HiddenFor(m => m.CurrentCityID, new { @id = "CityID" }) <div hidden="hidden" id="PaymentCBmsg"></div> </div> </div> </div> </div> <div class="panel panel-default" id="Button"> <span style="color:Red">* Required Fields</span> <div class="col-xs-12 col-sm-10 col-md-8 pull-right"> <button type="button" class="btn btn-default pull-right" onclick="Validate('UPDATE')" id="Update">Update</button> <button type="button" class="btn btn-default pull-right" onclick="Validate('EDIT')" id="Edit">Edit</button> </div> </div> <div hidden="hidden"> <button type="button" class="btn btn-default" id="btnClear" onclick="Clear();" style="display: none;">Clear</button> <a href="@Url.Action("EDIT_MERCHANT", "MERCHANT_Permission");" hidden="hidden" id="anClear"></a> </div> </div> </div> </fieldset> </div> } Server side code if (objMerchant.CurrentCityID != 0 && objMerchant.CurrentCityID != objMerchant.CityID) objMerchant.CityID = objMerchant.CurrentCityID; if (objMerchant.CurrentStateID != 0 && objMerchant.CurrentStateID != objMerchant.StateID) objMerchant.StateID = objMerchant.CurrentStateID; if (objMerchant.CurrentCountryID != 0 && objMerchant.CurrentCountryID != objMerchant.CountryID) objMerchant.CountryID = objMerchant.CurrentCountryID; 匹配,即后面的字符串开头。