I followed Ajb's recomendation here Splitting String using RegEx in Android and did it like this:
@SuppressWarnings("JavaDoc")
public class DBUtility extends SQLiteOpenHelper {
private static final String DB_NAME = "Angler.sqlite3";
private static final int DB_VERSION = 1; //minimal 1, 0 nicht zulässig.
private String DB_PATH_COMPLETE;
private String DB_PATH;
private String DB_PATH_DISPLAY;
private SQLiteDatabase SQL_DB;
private Context myContext;
private String displayText;
private BufferedReader Rdr;
private char[] SQLstringAr;
private String SQLstatementFile;
private Cursor cur;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DBUtility(Context context) {
// Kann nicht private werden
super(context, DB_NAME, null, DB_VERSION); //3. Parameter Wert null ist die CursorFactory
myContext = context;
DB_PATH = myContext.getDatabasePath(DB_NAME).getPath();
String RegEx = "((/data/data/(w+\\.w+/w+/w+/))w+\\.w+)"; // /data/data/wolfgang_jakobi.anglerapp/databases/Angler.sqlite3
Pattern p = Pattern.compile(RegEx);
Matcher m = p.matcher(DB_PATH);
String [] Groups = new String[3];
if (m.find()) {
Groups[0] = m.group(1); // /data/data/wolfgang_jakobi.anglerapp/databases/Angler.sqlite3
Groups[1] = m.group(2); // /data/data/wolfgang_jakobi.anglerapp/databases/
Groups[2] = m.group(4); // wolfgang_jakobi.anglerapp/databases/
}
DB_PATH_COMPLETE = Groups[0];
DB_PATH = Groups[1];
DB_PATH_DISPLAY = Groups[2];
SQL_DB = super.getReadableDatabase();
}
Alas the condition m.find() becomes not true so the code continues after the condition-bracket and all my Groups keep empty, I have no idea, why.
Thank you for your recomendations.
Wolfgang
I think your regex is wrong.
"((/data/data/(w+\\\\.w+/w+/w+/))w+\\\\.w+)"
It looks like you're trying to use the word character predefined character class , but, as written, you're just looking for w
characters. That is, you need to use \\\\w
instead of w
in your pattern.
"((/data/data/(\\w+\\.\\w+/\\w+/\\w+/))\\w+\\.\\w+)"
I developed software with regular expressions in C++ and ABAP, alas I did not follow here in Android Studio (JAVA) the same strategy: The known pattern-groups one has to set litarally into the regular expression and later on one can replace gradually every group by wildcards, in the present problem I used the wildcard "\\w+" as Ben recomended.
Doing this I discovered, that my grouping was erronious, because the 3rd "w+" was superflouous, but I never tried out if this error had any influence.
Here my new code, where I left every step of replacement by wildcards as a comment:
@SuppressWarnings("JavaDoc")
public class DBUtility extends SQLiteOpenHelper {
private static final String DB_NAME = "Angler.sqlite3";
private static final int DB_VERSION = 1; //minimal 1, 0 nicht zulässig.
private String DB_PATH_COMPLETE;
private String DB_PATH;
private String DB_PATH_DISPLAY;
private SQLiteDatabase SQL_DB;
private Context myContext;
private String displayText;
private BufferedReader Rdr;
private char[] SQLstringAr;
private String SQLstatementFile;
private Cursor cur;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DBUtility(Context context) {
// Kann nicht private werden
super(context, DB_NAME, null, DB_VERSION); //3. Parameter Wert null ist die CursorFactory
myContext = context;
DB_PATH = myContext.getDatabasePath(DB_NAME).getPath();
//String RegEx = "((/data/data/(wolfgang_jakobi\\.anglerapp/databases/))Angler\\.sqlite3)";
// /data/data/ wolfgang_jakobi .anglerapp/databases/Angler.sqlite3
//String RegEx = "((/data/data/(\\w+\\.anglerapp/databases/))Angler\\.sqlite3)";
//String RegEx = "((/data/data/(\\w+\\.\\w+/databases/))Angler\\.sqlite3)";
//String RegEx = "((/data/data/(\\w+\\.\\w+/\\w+/))Angler\\.sqlite3)";
//String RegEx = "((/data/data/(\\w+\\.\\w+/\\w+/))\\w+\\.sqlite3)";
String RegEx = "((/data/data/(\\w+\\.\\w+ /\\w+/))\\w+\\.\\w+)";
// previous version "((/data/data/( w+\\. w+/w+/ w+/))w+\\.w+)"
Pattern p = Pattern.compile(RegEx);
Matcher m = p.matcher(DB_PATH);
String [] Groups = new String[3];
if (m.find()) {
Groups[0] = m.group(1); // /data/data/wolfgang_jakobi.anglerapp/databases/Angler.sqlite3
Groups[1] = m.group(2); // /data/data/wolfgang_jakobi.anglerapp/databases/
Groups[2] = m.group(3); // wolfgang_jakobi.anglerapp/databases/
}
DB_PATH_COMPLETE = Groups[0];
DB_PATH = Groups[1];
DB_PATH_DISPLAY = Groups[2];
SQL_DB = super.getReadableDatabase();
}
This code works fine.
Greetings Wolfgang
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.