Extract string...

W

Wandy Tang

Hi,

I would like to extract string from this situation,
@phone:12345678@ or @phone:123456789@.
How can I retrieve string from between [@phone:mad:]?
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
 
I

Ike

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;
}
 
S

Sudsy

Wandy said:
Hi,

I would like to extract string from this situation,
@phone:12345678@ or @phone:123456789@.
How can I retrieve string from between [@phone:mad:]?
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!
 
W

Wandy Tang

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 said:
Wandy said:
Hi,

I would like to extract string from this situation,
@phone:12345678@ or @phone:123456789@.
How can I retrieve string from between [@phone:mad:]?
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!
 
F

Filip Larsen

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:mad:]?
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top