[android-beginners] Re: Regular expressions help

2009-10-23 Thread Jeffrey Blattman




hi,

i think it has to do with the multiline flag. that causes it to eat the
newline (read the javadocs).  i don't see any neat way to do it, other
than treating whitespace and newlines separately. for example,
something lime

    String s1 = "hello\n  hello\n\n    hello\n hi\n";
    Pattern p = Pattern.compile("^[ \t]+|[ \t]+$|\n$",
Pattern.MULTILINE);
    Matcher m = p.matcher(s1);
    s1 = m.replaceAll("");

On 10/23/09 9:16 AM, jax wrote:

  
I am trying to strip all whitespace from "each line" in a EditText
View.  The _expression_ I am using is this

String result =	Pattern.compile("^\\s+|\\s+$",
Pattern.MULTILINE).matcher(input).replaceAll("");

This works for the following input


hello
  hello
   hello
 hi

This will produce:
hello
hello
hello
hi


but

hello
  hello

   hello
 hi

will produce:
hello
hellohello
hi

Notice the new line (\n).  This is causing the problem.  What I want
is:
hello
hello
hello
hi

Any ideas about how to fix this?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---

  


-- 





[android-beginners] Re: Regular expressions help

2009-10-23 Thread jax

ok thanks,

as I understand \s matches all whitespace characters as there are some
extra unicode whitespace characters in other languages (not part of
ASCII).  Maybe it is not worth worrying about these characters though.

I will try you solution above.



On Oct 23, 11:40 pm, Jeffrey Blattman 
wrote:
> hi,
>
> i think it has to do with the multiline flag. that causes it to eat the
> newline (read the javadocs).  i don't see any neat way to do it, other
> than treating whitespace and newlines separately. for example, something
> lime
>
> /        String s1 = "hello\n  hello\n\n    hello\n hi\n";
>          Pattern p = Pattern.compile("^[ \t]+|[ \t]+$|\n$",
> Pattern.MULTILINE);
>          Matcher m = p.matcher(s1);
>          s1 = m.replaceAll("");/
>
> On 10/23/09 9:16 AM, jax wrote:
>
>
>
> > I am trying to strip all whitespace from "each line" in a EditText
> > View.  The expression I am using is this
>
> > String result =    Pattern.compile("^\\s+|\\s+$",
> > Pattern.MULTILINE).matcher(input).replaceAll("");
>
> > This works for the following input
>
> > 
> > hello
> >        hello
> >     hello
> >   hi
> > 
> > This will produce:
> > hello
> > hello
> > hello
> > hi
>
> > but
> > 
> > hello
> >        hello
>
> >     hello
> >   hi
> > 
> > will produce:
> > hello
> > hellohello
> > hi
>
> > Notice the new line (\n).  This is causing the problem.  What I want
> > is:
> > hello
> > hello
> > hello
> > hi
>
> > Any ideas about how to fix this?
>
> --
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~--~~~~--~~--~--~---