One question about Regular Expression!!!
I need to creat such a regular expression to match the format "[ ][ ][ ]".
For example, there is a context,
(1), " The project manager defines [1][0.400][+goals] for iterations."
Suppose that there are some spaces or "\n" characters in this way,
(2), " The project manager defines [1 ][ 0.400]
[+goals] for iterations."
If the pattern match the format succefully, (2) strings should be replaced by (1)strings, in order words, the format of (1) is what I need finally,
I had ever tried creating a regular expression likes \\[([^\n\s]]+)\\]\\[([^\n\s]]+)\\]\\[([^\n\s]]+)\\] , but it does not work well!
DO YOU HOW TO IMPLEMENT IT IN JAVA?
Thanks for your any reply!
[727 byte] By [
hairena] at [2007-11-26 12:20:39]

# 1
> If the pattern match the format succefully, (2)
> strings should be replaced by (1)strings, in order
> words, the format of (1) is what I need finally,
>
>
I don't understand! Matching the pattern is easy but could you elaborate on what you want to do with the result of the match?
# 2
Not sure if I get your question is this too simplistic?
public static void main(String[] args)
{
String s = "[ 1 ] [ 0.400 ]\n[ +goals]";
System.out.println( "Before: " + s );
s = s.replaceAll( "\\s", "" );
System.out.println( "After: " + s );
}
Note: This will also get rid of spaces inbetween words. is that a problem?
# 3
I think the OP means that if you encounter things that look like step 2, where there is blank space inside the [ ], like [data], to just trim all the whitespace out so you end up with just [data].
# 4
What I really need is that, via the regular expression, all the spaces and \n characters in square brackets [ and ], ] and [, will be thrown away.
For example,
Original:
1) "The project manager defines [1 ] [
0.400]
[+goals] for iterations with the support"
After matching:
2) "The project manager defines [1][0.400][ [+goals] for iterations with the support"
String 2) is what I need finally!
Thanks for your any reply!
# 5
> What I really need is that, via the regular
> expression, all the spaces and \n characters in
> square brackets [ and ], ] and [, will be thrown
> away.
>
> For example,
>
> Original:
> 1) "The project manager defines [1 ] [
>0.400]
> [+goals] for iterations with the support"
>
> After matching:
> 2) "The project manager defines [1][0.400][ [+goals]
> for iterations with the support"
>
> String 2) is what I need finally!
>
> Thanks for your any reply!
Well I gave you the answer to that one already :-)
If you need to preserve the spaces in between words use this one. I'm sure there's a better way to do it, I'm no RegEx master.
public static void main(String[] args)
{
String s = "[ 1 ] [ 0.400 ]\n[ +go als]";
System.out.println( "Before: " + s );
System.out.println( "\n\n" );
s = s.replaceAll( "\\[\\s+", "[" );
s = s.replaceAll( "\\s+\\]", "]" );
s = s.replaceAll( "\\]\\s+\\[", "][" );
System.out.println( "After: " + s );
}
# 6
Thanks for your reply, and I understand your code completely. But, using regax expression could be a better solution in my situation. : )
# 7
> Thanks for your reply, and I understand your code
> completely. But, using regax expression could be a
> better solution in my situation. : )
I thought I was writing regular expressions.....What more do you want? Sure you can probably condense those 3 lines into one line, but they're certainly regular expressions.
# 8
I would use
import e.util.*;
import java.util.*;
import java.util.regex.*;
class Filter extends Rewriter
{
public Filter()
{
super("\\[([^\\]]*)\\]");
}
public String replacement()
{
final String value = group(1);
return "[" + value.replaceAll("\\s+","") + "]";
}
}
public class Fred907_1
{
public static void main(String[] args) throws Exception
{
final String line = " The project manager defines [ \n1 ] [ \t\r0.400 ]\n[ +goals] for iterations.";
final Filter filter = new Filter();
System.out.println(filter.rewrite(line));
}
}
using Rewriter from http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html .
# 9
> I would use
> > import e.util.*;
> import java.util.*;
> import java.util.regex.*;
>
> class Filter extends Rewriter
> {
>public Filter()
> {
> super("\\[([^\\]]*)\\]");
>
> public String replacement()
>{
>final String value = group(1);
>return "[" + value.replaceAll("\\s+","") + "]";
> }
>
>
> public class Fred907_1
> {
> public static void main(String[] args) throws
> Exception
>{
> final String line = " The project manager
> defines [ \n1 ] [ \t\r0.400 ]\n[ +goals] for
> iterations.";
>final Filter filter = new Filter();
> System.out.println(filter.rewrite(line));
>}
>
>
> using Rewriter from
> http://elliotth.blogspot.com/2004/07/java-implementati
> on-of-rubys-gsub.html .
no no, you need to use regular expressions ;-)
# 10
> > no no, you need to use regular expressions ;-):-) OK, I'll just delete all the code except for the two regular expressions!