cancel
Showing results for 
Search instead for 
Did you mean: 

replace < > ( ) and parsing

Former Member
0 Kudos

Q 1)

i have a content content , i want to eliminate the < > ( )

it works fine for < > but not ( )

i will get this if i include the ( ) ..pls advice

java.util.regex.PatternSyntaxException: Unclosed group near index 1

(

^

Q2)

if i want to write a method to get the mail address

xxxx@xxxx.com how to i write this method and store it in a string array...

String[] strAdd =parse(newstrContent);


public class test2 {

	public static void main(String[] args) {
String strContent="The original message was received at Mon, 9 Apr 2007 16:08:36 +0800" +
  "----- The following addresses had permanent fatal errors -----" +
  "<robinsonamerereee@hotmail.com>" +
  " ----- Transcript of session follows -----" +
  "... while talking to mx1.hotmail.com" +
  ">>> RCPT To:<robinsonamerereee@hotmail.com>" +
  "<<< 550 Requested action not taken: mailbox unavailable";
		
		String newContent=replace(strContent);
		System.out.println(newContent);
	}
	
private static String replace(String strContent){
String newstrContent = "";

newstrContent=strContent.replaceAll("<","");
newstrContent=newstrContent.replaceAll(">","");
//newstrContent=newstrContent.replaceAll("(","");
//newstrContent=newstrContent.replaceAll(")","");
System.err.println(newstrContent);
System.err.println("Mail Delivery Error Content:" +newstrContent.trim());
		return newstrContent.trim();
	}	

}

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

yzme,

Symbols <b>(</b> and <b>/)</b> has special meaning in regular expressions, they are boundaries of groups. So just escape this symbol:


newstrContent=newstrContent.replaceAll("\(","");
newstrContent=newstrContent.replaceAll("\)","");

Valery Silaev

SaM Solutions

http://www.sam-solutions.net

Former Member
0 Kudos

2)

newstrContent="The original message was received at Mon, 9 Apr 2007 16: 08: 36 +0800- - - - - The following addresses had permanent fatal errors - - - - - robinsonamerereee@hotmail.com - - - - - Transcript of session follows - - - - - ... while talking to mx1.hotmail.com RCPT To: robinsonamerereee@hotmail.com 550 Requested action not taken: mailbox unavailable";

if i want to write a method to get the mail address

xxxx@xxxx.xxx how to i write this method and store it in a string array...

there will be space in between.....

RCPT To: <space> robinson@hotmail <space> 550 Requested

String[] strAdd =parse(newstrContent);

Message was edited by:

yzme yzme

Message was edited by:

yzme yzme

Message was edited by:

yzme yzme

Former Member
0 Kudos

Hi,

String regex = "(\w+)@(\w+\.)(\w+)(\.\w+)*";
Pattern pattern = Pattern.compile(regex);
String targetString = "<your string here>";
Matcher matcher = pattern.matcher(targetString);

while (matcher.find()) {
System.out.println("Found a match: " + matcher.group());
}

Regards,

Satyajit.

Former Member
0 Kudos

Satyajit,

Your RegExp does not accept plenty of valid mail addresses with dots and hyphens like some.person@no-spam.com

Yzme,

Try this class:


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

public static class EMailScanner {
  final private static Pattern EMAIL = Pattern.compile(
    "\w([\w\.\+\-]*\w)?@(((\w([\w\-]*\w)?\.)+[a-zA-Z]{2,6})|([a-zA-Z]([\w\-]*\w)?))"
  );

  final public static EMailScanner INSTANCE = new EMailScanner(); 

  public List scanAll(final CharSequence input) {
    final List result = new ArrayList();
    scan(input, result);
    return result;
  }

  public Set scanUnique(final CharSequence input) {
    final Set result = new TreeSet();
    scan(input, result);
    return result;
  }

  public Set scanUniqueOrdered(final CharSequence input) {
    final Set result = new LinkedHashSet();
    scan(input, result);
    return result;
  }

  public void scan(final CharSequence input, final Collection result) {
    final Matcher matcher = EMAIL.matcher( input );
    while ( matcher.find() ) result.add( matcher.group() );
  }
}

Usage;


final String input = 
  "Message sent from some.person@domain.net to another_guy@who.knows.com via no-spam@gateway"
final Collection emails = EMailScanner.INSTANCE.scanUniqueOrdered(input);
System.out.println( emails );

Valery Silaev

SaM Solutions

http://www.sam-solutions.net

Former Member
0 Kudos

if i parse this string i will get 3 mail address , which is

johnson@hotmail.com , robinsonameree@hotmail.com, robinsonameree@hotmail.com

how do i do a comparison between String[] to eliminate to 2...

String strContent="The original message was received at Mon, 9 Apr 2007 16:08:36 +0800" +

"----- The following addresses had johnson@hotmail.com permanent fatal errors -


" +

"<robinsonamerereee@hotmail.com>" +

" -


Transcript of session follows -


" +

"... while talking to mx1.hotmail.com" +

">>> RCPT To:<robinsonameree@hotmail.com>" +

"<<< 550 Requested action not taken: mailbox unavailable";

String[] arrResult=parseMailAddress(strContent);

for(int i=0;i<arrResult.length;i++){

System.err.println(arrResult<i>.toString());

}

public static String[] parseMailAddress(String newStrContent){

String[] strArrMail=new String[]{};

String str="";

String regex = "(
w)@(
w

.)(
w)(
.
w
)*";

Pattern pattern = Pattern.compile(regex);

//String targetString = "<your string here>";

Matcher matcher = pattern.matcher(newStrContent);

while (matcher.find()) {

System.out.println("Found a match: " + matcher.group());

str=str matcher.group()",";

}

strArrMail=str.split(",");

//strArrMail=matcher.group().split(",");

return strArrMail;

}

Former Member
0 Kudos

Hi,

Modify the <i>parseMailAddress</i> methof a bit.


public static String[] parseMailAddress(String newStrContent){
String[] strArrMail=new String[]{};
String str="";

//Use the pattern suggested by Valery. It's more complete.
String regex = "(\w+)@(\w+\.)(\w+)(\.\w+)*";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(newStrContent);

while (matcher.find()) {
String found = matcher.group();
System.out.println("Found a match: " + found);
str = str.replaceAll(found+",","");
str=str +found+","; 
}

Regards,

Satyajit.

Former Member
0 Kudos

Yzme,

My code hadles this automatically, try either of scanUnique or scanUniqueOrdered.

VS

Answers (0)