Java code to extract strings in the specified format

Source: Internet
Author: User

Importjava.util.stack;//dehong Hongju oldjiang DP Public classRedchrysanthemum {Private StaticString result = ""; Private Staticstack<string> stack =NewStack<>();  Public Static voidMain (string[] args) {//" 4[b]3[ac]" is "BBBBACACAC" after decompression//" 2[d3[c]" "DCCCDCCC" after decompressionString str = "EF10[D3[CC]]";    System.out.print (Process (str)); }    Private Staticstring Process (String str) {string in, out; intLength =str.length ();  for(inti = 0; i < length; i++) { in=string.valueof (Str.charat (i)); //is not equal to the right parenthesis in the stack            if(!in.equals ("]") ) {Stack.push (in); } Else{String tmpstr= ""; String Tar= ""; out=Stack.pop (); //the stacks are all [alphanumeric, get the numbers together, pop out one, and then go to the following loop. //If you have more than one letter, you will continue to walk this loop, the numbers go below.                  while(!out.equals ("[") ) {Tmpstr= out +Tmpstr; //pop out one, continue to judge, is the letter on the popout =Stack.pop (); }                //number 2[d3[c]]String numstr = "";  while(Stack.peek (). CharAt (0) >= ' 0 ' && stack.peek (). CharAt (0) <= ' 9 ') {                    //access to the top of the stack, is the number on the stitching, and then jumped one. Numstr = Stack.peek () +Numstr;                    Stack.pop (); if(Stack.isempty ()) { Break; }                }                //get the number left in the left bracket                intnum =Integer.parseint (NUMSTR);  for(intj = 0; J < num; J + +) {tar= Tar +Tmpstr;            } stack.push (TAR); }        }         while(Stack.size () > 0) {String finalstr=Stack.pop (); Result= Finalstr +result; }        returnresult; }}

Java code to extract strings in the specified format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.