java - Remove all characters after certain sequence -


i wanted remove characters after end of first number sequence.

edit: (sorry ambiguity)

  1. the string may or may not start letters, in case starts letters should preserved.
  2. after first sequence of numbers, should removed.

    e.g. qwee1232rty -> qwe1232

    123rty -> 123

    12342 -> 12342

    str.replaceall("[^a-z][^0-9]+",""); 

this should work it's giving unexpected output , not printing repeated "2".

my ideone code attempt , it's respective outputs

    import java.util.*;     import java.lang.*;     import java.io.*;         class ideone     {         public static void main (string[] args) throws java.lang.exception         {             string str = "bctc27452asdfccc";             system.out.println(str.replaceall("[^a-z0-9]",""));                // bctc27452asdfccc             system.out.println(str.replaceall("[^a-z0-9]$",""));                // bctc27452asdfccc             system.out.println(str.replaceall("[^a-z][^0-9]",""));                // bctc2745sdfccc             system.out.println(str.replaceall("[^a-z][^0-9]+",""));                  // closest output - "bctc2745" (why 2nd "2" not printed)             system.out.println(str.replaceall("[^a-z][^0-9]*",""));                // bctc             system.out.println(str.replaceall("[^a-z][^0-9+]",""));                // bctc2745sdfccc             system.out.println(str.replaceall("[^a-z][^0-9*]",""));                 // bctc2745sdfccc                  system.out.println("expected output: btc27452");          }     } 

you mean replace after sequence of letters followed sequence of digits? , replace string match only:

str.replaceall("^([a-z]+[0-9]+).*","$1") 

so sequence of @ least 1 upper case character ([a-z]+) followed sequence of @ least 1 digit ([0-9]+) starting @ beginning of input (^) , followed ( .*). capture sequence group , replace string group ($1 in replacement string refers first capturing group).


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -