Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Extract string...

Reply
Thread Tools

Extract string...

 
 
Wandy Tang
Guest
Posts: n/a
 
      07-16-2004
Hi,

I would like to extract string from this situation,
@phone:12345678@ or @phone:123456789@.
How can I retrieve string from between [@phone:@]?
I do not want to read character one by one,
I am guessing I can use regular expression to achieve this situation...
Appreciate any help, TIA.

Regards,
Wandy Tang


 
Reply With Quote
 
 
 
 
Ike
Guest
Posts: n/a
 
      07-16-2004
String s[] = getArgs("@phone:12345678@ or @phone:123456789@.", "@");
//this gives you an array of strings.....
phone:12345678
or
phone:12345678
//Then merely call it again with one of the strings like "phone:12345678",
this time parsing on the colon:
String ss[] = getArgs(s[1],":");
//and now in ss[1] you should have something like "12345678" //Ike

final public String[] getArgs(String parameter, String delimiter){
String args[];
int nextItem=0;
StringTokenizer stoke=new StringTokenizer(parameter,delimiter);
args=new String[stoke.countTokens()];
while(stoke.hasMoreTokens()){
args[nextItem]=stoke.nextToken();
nextItem=(nextItem+1)%args.length;
}
return args;
}

"Wandy Tang" <(E-Mail Removed)> wrote in message
news:cd8cj2$(E-Mail Removed)...
> Hi,
>
> I would like to extract string from this situation,
> @phone:12345678@ or @phone:123456789@.
> How can I retrieve string from between [@phone:@]?
> I do not want to read character one by one,
> I am guessing I can use regular expression to achieve this situation...
> Appreciate any help, TIA.
>
> Regards,
> Wandy Tang
>
>



 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      07-16-2004
Wandy Tang wrote:
> Hi,
>
> I would like to extract string from this situation,
> @phone:12345678@ or @phone:123456789@.
> How can I retrieve string from between [@phone:@]?
> I do not want to read character one by one,
> I am guessing I can use regular expression to achieve this situation...
> Appreciate any help, TIA.


String s = "@phone:123456789@";
System.out.println( s.substring( 1, s.length() - 1 ).split( ":" )[1] );

Of course that's starting to look like APL. It also doesn't perform the
kind of checking you require for robust, production-level code. But it
works!

 
Reply With Quote
 
Wandy Tang
Guest
Posts: n/a
 
      07-16-2004
Thanks all for your kindly response.

Actually, I am trying to do this:

my phone no: @phone:12345678@ or @phone:1234567890@

to code like this use in wml application:

my phone no: <a href="wtai://wp/mc;12345678">12345678</a> or <a
href="wtai://wp/mc;1234567890">1234567890</a>

Any help appreciated. Thanks all.


"Sudsy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Wandy Tang wrote:
> > Hi,
> >
> > I would like to extract string from this situation,
> > @phone:12345678@ or @phone:123456789@.
> > How can I retrieve string from between [@phone:@]?
> > I do not want to read character one by one,
> > I am guessing I can use regular expression to achieve this situation...
> > Appreciate any help, TIA.

>
> String s = "@phone:123456789@";
> System.out.println( s.substring( 1, s.length() - 1 ).split( ":" )[1] );
>
> Of course that's starting to look like APL. It also doesn't perform the
> kind of checking you require for robust, production-level code. But it
> works!
>



 
Reply With Quote
 
Filip Larsen
Guest
Posts: n/a
 
      07-16-2004
Wandy Tang wrote

> I would like to extract string from this situation,
> @phone:12345678@ or @phone:123456789@.
> How can I retrieve string from between [@phone:@]?
> I do not want to read character one by one,
> I am guessing I can use regular expression to achieve this

situation...

Indeed you can use regular expressions. For instance, the following
class will return the phone numbers from 4 to 10 digits long, or null if
there is no match.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneMatcher {

private final Pattern pattern = Pattern.compile("@phone\\d{4,10})@");

public String getPhone(String input) {
Matcher matcher = pattern.matcher(input);
return matcher.matches() ? matcher.group(1) : null;
}

}


Read more about regex in the javadoc for Pattern and Matcher.


Regards,
--
Filip Larsen


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
how do you extract carry, borrow and overflow from an adder in vhdl? Rune Christensen VHDL 4 01-22-2005 11:17 PM
how to extract LaTeX macro arguments? ivo welch Perl 4 12-26-2003 03:42 AM
extract strings between alternating text Lydia Shawn Perl 2 08-10-2003 10:01 AM
extract from html Lydia Shawn Perl 2 08-07-2003 08:38 PM



Advertisments