Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2010

Tip: Looking for answers? Try searching our database.

Write two regular expression to one

Thread view: 
The87Boy - 05 Feb 2010 22:07 GMT
I do not know if this is the right place for this question, but I
think so
I want to write two regular expression which is:
String st = str.replaceAll("\\n", "").replaceAll("\\r", "");

Can anybody help me with this little question?
Lew - 05 Feb 2010 22:43 GMT
> I do not know if this is the right place for this question, but I
> think so
> I want to write two regular expression which is:
> String st = str.replaceAll("\\n", "").replaceAll("\\r", "");
>
> Can anybody help me with this little question?

  String st = str.replaceAll( "[\\n\\r]", "" );

Signature

Lew

The87Boy - 05 Feb 2010 22:50 GMT
> > I do not know if this is the right place for this question, but I
> > think so
[quoted text clipped - 4 lines]
>
>    String st = str.replaceAll( "[\\n\\r]", "" );

Thx for the help Lew
Roedy Green - 06 Feb 2010 07:34 GMT
>I do not know if this is the right place for this question, but I
>think so
>I want to write two regular expression which is:
>String st = str.replaceAll("\\n", "").replaceAll("\\r", "");
>
>Can anybody help me with this little question?

Are you looking for code that will efficiently remove all \n and \r
characters in a string?

A flat-footed loop like this will do it faster than any regex.

private static string unwrap ( String s )
 {
 if ( s.indexOf( '\n' ) < 0 && s.indexOf( '\r' ) < 0 )
  {
  return s;
  }
 StringBuilder sb = new StringBuilder( s.length() );
 for ( int i=0; i<s.length(); i++ )
   {
   char c = s.charAt( i );
   if ( !( c == '\n' || c == '\r' ) )
      {
      sb.append( c );
      }
    }
 return s.toString();
 }
I think however, you actually probably want to replace \r\n with
space.

if ( c == '\n' || c == '\r' )
      {
      sb.append( ' ' );
      }
else
      {
      sb.append( c );
      }
Signature

Roedy Green Canadian Mind Products
http://mindprod.com

You can’t have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy

Roedy Green - 06 Feb 2010 07:38 GMT
>I do not know if this is the right place for this question, but I
>think so
>I want to write two regular expression which is:
>String st = str.replaceAll("\\n", "").replaceAll("\\r", "");
>
>Can anybody help me with this little question?

If you want to use regexes, and replace different elements with
different expressions, you need to use

Matcher.appendReplacement and a StringBuffer.
Signature

Roedy Green Canadian Mind Products
http://mindprod.com

You can’t have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy

Roedy Green - 06 Feb 2010 19:55 GMT
>String st = str.replaceAll("\\n", "").replaceAll("\\r", "");

to answer your question literally, all you need is a regex expression
that will match \n or \r as your first argument.  You could do that
with
"\\n|\\r" or "[\\n\\r]"
Signature

Roedy Green Canadian Mind Products
http://mindprod.com

Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.