You are viewing a plain text version of this content. The canonical link for it is here.
Posted to regexp-dev@jakarta.apache.org by Marc briand <ma...@yahoo.com> on 2001/04/09 15:48:13 UTC
Pbm with a RegExp
Dear All,
I have a probleme using a RegExp with the
com.oroinc.text.regex.* classes.
I have to do the following (simplified):
I have a file with the following content:
dn:cn=,ou=,o=fr
I have to transform this file like the following:
dn:cn=user.ou=org,o=fr
I tried with the RegExp package but I have a Pbm since
A PatternCompiler instance is used to compile the
string representation (either as a String or char[])
of a regular expression into a Pattern instance.
I ahve the following code (It is a simple sample code
used only to understand and then resolve the pbm):
****************************************************
import com.oroinc.text.regex.*;
import java.util.*;
public final class essai2 {
public static final void main(String args[]) {
int limit, interps, i;
PatternMatcher matcher = new Perl5Matcher();
Pattern pattern = null;
PatternCompiler compiler = new Perl5Compiler();
String regularExpression, input, sub, result;
limit = Util.SUBSTITUTE_ALL;
interps = Util.INTERPOLATE_ALL;
HashMap hm = new HashMap();
hm.put("cn","user");
hm.put("ou","org");
Set set = hm.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext())
{
String trash = "cn=";
Map.Entry me = (Map.Entry) iterator.next();
System.out.println(me.getKey() + "/" +
me.getValue());
String key = (String)me.getKey();
String value =(String)me.getValue();
regularExpression =key;
sub=key=value ;//PBM HERE
input = "cn=,ou=,o=fr";
try {
pattern = compiler.compile(regularExpression);
System.out.println("substitute regex: " +
regularExpression);
} catch(MalformedPatternException e){
System.err.println("Bad pattern.");
System.err.println(e.getMessage());
System.exit(1);
}
// Perform substitution and print result.
result = Util.substitute(matcher, pattern, sub,
input, limit, interps);
System.out.println("result: " + result);
}
}
}
******************************************************
I have the following result:
<cn/user
substitute regex: cn
result: user=,ou=,o=fr
ou/org
substitute regex: ou
result: cn=,org=,o=fr>
Instead of the following:
<cn/user
substitute regex: cn
result: cn=user=,ou=,o=fr
ou/org
substitute regex: ou
result: cn=user,ou=org,o=fr>
Can anyone help me?
Thanks in advance.
Best regards,
Marc.
__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://personal.mail.yahoo.com/
Re: Pbm with a RegExp
Posted by Michael McCallum <mi...@spinsoftware.com>.
This should be sent to oro-users@jakarta.apache.org
On 9 Apr 2001, at 6:48, Marc briand wrote:
> Dear All,
> I have a probleme using a RegExp with the
> com.oroinc.text.regex.* classes.
>
> I have to do the following (simplified):
> I have a file with the following content:
>
> dn:cn=,ou=,o=fr
>
>
> I have to transform this file like the following:
> dn:cn=user.ou=org,o=fr
>
> I tried with the RegExp package but I have a Pbm since
> A PatternCompiler instance is used to compile the
> string representation (either as a String or char[])
> of a regular expression into a Pattern instance.
>
> I ahve the following code (It is a simple sample code
> used only to understand and then resolve the pbm):
>
> ****************************************************
> import com.oroinc.text.regex.*;
> import java.util.*;
>
> public final class essai2 {
>
>
> public static final void main(String args[]) {
>
>
> int limit, interps, i;
> PatternMatcher matcher = new Perl5Matcher();
> Pattern pattern = null;
> PatternCompiler compiler = new Perl5Compiler();
> String regularExpression, input, sub, result;
>
>
>
> limit = Util.SUBSTITUTE_ALL;
> interps = Util.INTERPOLATE_ALL;
>
>
>
>
>
>
> HashMap hm = new HashMap();
>
>
> hm.put("cn","user");
> hm.put("ou","org");
> Set set = hm.entrySet();
>
> Iterator iterator = set.iterator();
>
> while (iterator.hasNext())
> {
> String trash = "cn=";
> Map.Entry me = (Map.Entry) iterator.next();
> System.out.println(me.getKey() + "/" +
> me.getValue());
> String key = (String)me.getKey();
> String value =(String)me.getValue();
> regularExpression =key;
> sub=key=value ;//PBM HERE
You might try sub = key + "=" + value;
> input = "cn=,ou=,o=fr";
>
>
>
> try {
> pattern = compiler.compile(regularExpression);
> System.out.println("substitute regex: " +
> regularExpression);
> } catch(MalformedPatternException e){
> System.err.println("Bad pattern.");
> System.err.println(e.getMessage());
> System.exit(1);
> }
>
> // Perform substitution and print result.
> result = Util.substitute(matcher, pattern, sub,
> input, limit, interps);
> System.out.println("result: " + result);
>
> }
> }
> }
>
> ******************************************************
>
>
>
>
> I have the following result:
> <cn/user
> substitute regex: cn
> result: user=,ou=,o=fr
> ou/org
> substitute regex: ou
> result: cn=,org=,o=fr>
>
> Instead of the following:
> <cn/user
> substitute regex: cn
> result: cn=user=,ou=,o=fr
> ou/org
> substitute regex: ou
> result: cn=user,ou=org,o=fr>
>
>
>
> Can anyone help me?
>
> Thanks in advance.
>
> Best regards,
> Marc.
>
>
>
>
>
>
>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Get email at your own domain with Yahoo! Mail.
> http://personal.mail.yahoo.com/