Regular expressions help

by jax » Sat, 24 Oct 2009 00:17:38 GMT


Sponsored Links
 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?

--~--~---------~--~----~------------~-------~--~----~



Regular expressions help

by jax » Sat, 24 Oct 2009 11:15:16 GMT


 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:40pm, Jeffrey Blattman <jeffrey.blatt...@gmail.com>



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. Preferred Activity-Service communication pattern

I would suspect this Activity-Service communication pattern is being
implemented in a variety of ways with good reason.  Either the
developer is a newbie and found a way that works (probably from Mark's
book) or else they are strategically searching for a solution that
works specifically for their use case.  I am a newbie looking for the
specific case!

My application needs to have a daemon-like thread running to collect
statistics.  I chose the Service here because it looked like it has a
lesser chance of being killed-- especially when conditioned with the
setForeground(true).  Perhaps I could replace the service with a
Broadcast receiver and store my statistics in an Application static
structure as Mark suggests.  My UI Activity *does* depend on the
collected statistics for presentation and that Activity can be killed
and resurrected any number of times without causing a problem.
However, if the Service is stopped, the application will miss events
that statics are beg compiled over and the application will
effectively become useless!  Does the Android Application lifecycle
support the notion of a daemon thread that will not get booted out of
memory?  I have read about lifecycles and precious resources and my
service is as skinny as they come.


What strikes me as odd is how on the one hand Android touts being able
to run background tasks when all the while you are encouraged not to
do it!

I am currently updating the Activity UI from the Service via a stashed
Handler in the Application scope.  Generally my design is as follows:

AppStatistics extends Application and contains the current Activity
instance and stats
public class AppStatistics extends Application {
    private PlanTrak planTrak;
    // internal statistics here
PlanTrak is my UI
public class PlanTrak extends Activity {

    Handler guiHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        guiHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
          // blah, blah, UI updates
};
        ((AppStatistics) getApplication()).setPlanTrak(this);

    }
}
PlanTrakService is my aemonpublic class PlanTrakService extends Service {
     AppStatistics app;
    @Override public void onCreate() {
      super.onCreate();
      // I read this is supposed to raise the priority of this Service
to minimize it being killed
      setForeground(true);
    app = (AppStatistics) getApplication();

      // init the service here
      _startService();
    }

    @Override public void onDestroy() {
      super.onDestroy();
      _shutdownService();
    }

    private void _startService() {
      timer.scheduleAtFixedRate(
          new TimerTask() {
            public void run() {
              _monitorStats();
            }
          },
          0,
          UPDATE_INTERVAL);
      Log.i(getClass().getSimpleName(), "Timer started!!!");
    }

    private void _monitorStats() {
      app.blahStatsUpdate();
      Message message = Message.obtain();
      message.arg1=app.getBlahStatsUpdate();
      app.getPlanTrak().getGuiHandler().sendMessage(message);
    }

    private void _shutdownService() {
      if (timer != null) timer.cancel();
    }
}

--~--~---------~--~----~------------~-------~--~----~

2. Dialog callback from withing callback?

I'm trying to pop up an AlertDialog when someone long presses a list
item.  What I end up with is nested callback objects.  I'm not sure if
that is the problem but simple Alert dialog examples are not working
for me.  Here is what I want and is crashing:

smsContactsListView.setOnItemLongClickListener(new
OnItemLongClickListener() {
                @Override
                        public boolean onItemLongClick(AdapterView<?> parent, 
View view,
int position, final long id) {

                        new AlertDialog.Builder(getApplicationContext())
                        .setTitle("Alert")
                        .setMessage("Really Delete SMS Contact?")
                        .setPositiveButton("Ok", new 
DialogInterface.OnClickListener
() {
                                        @Override
                                        public void onClick(DialogInterface 
dialog, int which) {
                                                dbAdapter.removeSmsContact(id);
                                        smsContactsCursor.requery();
                                        }
                                })
                        .setPositiveButton("Cancel", new
DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface 
dialog, int which) {
                                        }
                                })
                        .show();

                        // return true to indicate we consumed the callback
                                return true;
                        }
                });


Here is what works (no dialog at all):

smsContactsListView.setOnItemLongClickListener(new
OnItemLongClickListener() {
                @Override
                        public boolean onItemLongClick(AdapterView<?> parent, 
View view,
int position, final long id) {

                        // This works for now
                        dbAdapter.removeSmsContact(id);
                        smsContactsCursor.requery();

                        // return true to indicate we consumed the callback
                                return true;
                        }
                });

--~--~---------~--~----~------------~-------~--~----~

3. Posts not appearing in group

4. emulator to run iphone apps on android

5. OpenGL and VBO buffers

6. Dialog Theme

7. Detecting hardware specs or phone model