简体   繁体   English

字符的正则表达式,

[英]Regular Expression for characters ,

I need to ask you something, about my problem.Try to imagine, that a have a string like: "23221323,213213,123213,,," 我需要问您一些有关我的问题的信息。试想一下,其中有一个字符串,例如:“ 23221323,213213,123213 ,,,”

I have been looking in several websites, but i dont find anything. 我一直在寻找几个网站,但没有找到任何东西。 I need a regular expression about how to remove the character(,) .. 我需要一个有关如何删除character(,)的正则表达式。

I mean, i want to remove the last , if its more that 1: 我的意思是,我要删除最后一个,如果它大于1:

Example: 例:

"2323,3434,2332" ==> its OK “ 2323,3434,2332” ==>可以

"3434,21321,45454,,,,==> BAD. you have to remove the last 3 , only 1 final is allowed. “ 3434,21321,45454 ,,, ==>不良。您必须删除最后3个,仅允许1个决赛。

Actually i have something in java, that works: 实际上我在Java中有一些东西可以工作:

  String sCadena="asd,";
            CharSequence cs1 = ",,";
            CharSequence cs2 = ",,,";
            CharSequence cs0 = ",";

            if(sCadena.contains(cs2)){
                sCadena=sCadena.substring(0, sCadena.length() - 3);
            }


             else if (sCadena.contains(cs1)){
                sCadena=sCadena.substring(0, sCadena.length() - 2);
            }


             else if  (sCadena.contains(cs0)){
                sCadena=sCadena.substring(0, sCadena.length() - 1);
            }



}       

But i want to make a regular expression to avoid this, because if the user enter a lot of (,), i have to implement more if to control this.... 但是我想做一个正则表达式来避免这种情况,因为如果用户输入很多(,),则如果要控制它,我必须实现更多。

Any ideas?? 有任何想法吗??

This should work for you : 这应该为您工作:

public static void main(String[] args) {
    String s = "23221323,213213,123213,,,";
    s = s.replaceAll(",+$",","); // replaces all trailing commas with a single one
    System.out.println(s);
    }

O/P : O / P:

23221323,213213,123213, 23221323,213213,123213,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM