How can I say a String matches my pattern [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how can I say that is my String match with this pattern: (install OS #name #version) which name and version can be any String without white space.

As you can use following Code:
// assuming parenthesis an sharp are pattern included:
String s = "(install OS #testname #testversion)";
if (s.matches("\\(install\\sOS\\s#\\S+\\s#\\S+\\)")) {
String[] splitted = s.split("\\s");
String name = splitted[2].replace("#", "");
String version = splitted[3].replace(")", "").replace("#", "");
System.out.println("name: " + name);
System.out.println("version: " + version);
}
// assuming parenthesis an sharp are both not pattern included:
s = "install OS testname2 testversion2";
if (s.matches("install\\sOS\\s\\S+\\s\\S+")) {
String[] splitted = s.split("\\s");
String name = splitted[2];
String version = splitted[3];
System.out.println("name: " + name);
System.out.println("version: " + version);
}
(if you need to allow mutliple whitespaces between parts, you have replace in both, split regex and match regex, \s with \s+)

Related

how to take input in ascii and print it in string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
import string
def main():
inString =input("enter any number to decode : ")
message=" "
for numStr in string.split(instring):
asciiNum = eval(numStr)
message = message+chr(asciiNum)
print("\n", message)
main()
You need to convert the input to int by using int() function because the input is a string and ASCII is a int
To get the ASCII value, you can use chr()
So, for example:
inString = input("enter any number to decode : ")
number_to_decode = int(inString)
print( chr( number_to_decode ) )
It should work:
def main():
instring = input("enter any number to decode : ")
message = " "
for numStr in str.split(instring):
asciiNum = eval(numStr)
message = message + chr(asciiNum)
print("\n", message)
Here is a Java soln,
class AsciiConvertor{
public static void main(String[] args) {
int asciiVal = 87;
String strValue = Character.toString((char) asciiVal);
System.out.println(strValue);
}
}
Output: W

How to get path parameters from path using Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a path /departments/{dept}/employees/{id}. How do I obtain dept and id from path /departments/{dept}/employees/{id}?
For example, I would like to obtain dept1 and id1 if path is /departments/dept1/employees/id1
I tried
String pattern1 = "departments/"
String pattern2 = "/employees"
Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2));
Matcher m = p.matcher(text);
while (m.find()) {
String a = m.group(1);
}
Is there a simpler way to obtain dept1 and id1? I would prefer not using string.split as I have different paths for which I want to obtain path parameters and I prefer not having a dependency on the index position of the path parameters.
Using Spring... or:
String url = /departments/{dept}/employees/{id}
/----none--/-dept-/---none---/-id-
Make a split of url and get the position of array 1 and 3:
String urlSplited = url.split("/");
String dept = urlSplited[1];
String id = urlSplited[3];
If you are using Spring framework, then you can use a class specifically for this purpose named AntPathMatcher and use its method extractUriTemplateVariables
So, you can have the following:
AntPathMatcher matcher = new AntPathMatcher();
String url = "/departments/dept1/employees/id1";
String pattern = "/departments/{dept}/employees/{id}";
System.out.println(matcher.match(pattern, url));
System.out.println(matcher.extractUriTemplateVariables(pattern, url).get("dept"));
System.out.println(matcher.extractUriTemplateVariables(pattern, url).get("id"));
If you prefer regix:
import org.junit.Test;
import java.util.regex.Pattern;
public class ExampleUnitTest {
#Test
public void test_method() throws Exception {
Pattern digital_group = Pattern.compile("[//]");
CharSequence line = "test/message/one/thing";
String[] re = digital_group.split(line);
for (int i=0;i<re.length;i++) {
System.out.println(re[i]);
}
}
} //END: class ExampleUnitTest
The output is:
test
message
one
thing
Process finished with exit code 0

How to use RegEx in Java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I would like to transfer the following code from Python to Java, but I get an error, while doing it:
import re
payload = re.search(
r'decrypt\.setPrivateKey\("(?P<privateKey>[^"]+)".*?'
r'decrypt\.decrypt\("(?P<cryptText>[^"]+)".*?'
r'document\.cookie="ipp_uid=(?P<ipp_uid>[^"]+)".*?'
r'document\.cookie="ipp_uid1=(?P<ipp_uid1>[^"]+)".*?'
r'document\.cookie="ipp_uid2=(?P<ipp_uid2>[^"]+)".*?'
r'url\s\+=\s"(?P<makeURL>.*?)"\;.*?'
r'salt="(?P<salt>[^"]+)"',
ret.content.decode('utf-8'),
re.MULTILINE | re.DOTALL
)
I have already tried the following code:
String patternString = "decrypt\\.setPrivateKey\\(\"(?P<privateKey>[^\"]+)\".*?\n"
+ " decrypt\\.decrypt\\(\"(?P<cryptText>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid=(?P<ipp_uid>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid1=(?P<ipp_uid1>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid2=(?P<ipp_uid2>[^\"]+)\".*?\n"
+ " url\\s\\+=\\s\"(?P<makeURL>.*?)\"\\;.*?\n"
+ " salt=\"(?P<salt>[^\"]+)\"";
Pattern payload = Pattern.compile(patternString);
String content = new String(html.getBytes(), "UTF-8");
Matcher m = payload.matcher(html);
if(m.find()){
System.out.println("Found: " + m.group(0));
}else{
System.out.println("not found");
}
... but I am getting this error:
java.util.regex.PatternSyntaxException: Unknown inline modifier near index 27
decrypt\.setPrivateKey\("(?P<privateKey>[^"]+)".*?
decrypt\.decrypt\("(?P<cryptText>[^"]+)".*?
document\.cookie="ipp_uid=(?P<ipp_uid>[^"]+)".*?
document\.cookie="ipp_uid1=(?P<ipp_uid1>[^"]+)".*?
document\.cookie="ipp_uid2=(?P<ipp_uid2>[^"]+)".*?
url\s\+=\s"(?P<makeURL>.*?)"\;.*?
salt="(?P<salt>[^"]+)"
^
at java.util.regex.Pattern.error(Pattern.java:1957)
at java.util.regex.Pattern.group0(Pattern.java:2896)
at java.util.regex.Pattern.sequence(Pattern.java:2053)
at java.util.regex.Pattern.expr(Pattern.java:1998)
at java.util.regex.Pattern.compile(Pattern.java:1698)
at java.util.regex.Pattern.<init>(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1028)
at fabian.site.MyModule.test(MyModule.java:76)
at fabian.site.MyModule.run(MyModule.java:61)
at fabian.thread.ThreadPool$PoolThread.run(ThreadPool.java:50)
Thank you for your help guys!!
Two things stand out to me:
Named capturing groups in Java are structured like (?<name>X), not (?P<name>X), so you should remove the Ps
The names cannot contain "_", so you should replace ipp_uid with something like ippUid (only letters and numbers)
String patternString = "decrypt\\.setPrivateKey\\(\"(?<privateKey>[^\"]+)\".*?\n"
+ " decrypt\\.decrypt\\(\"(?<cryptText>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid=(?<ippuid>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid1=(?<ippuid1>[^\"]+)\".*?\n"
+ " document\\.cookie=\"ipp_uid2=(?<ippuid2>[^\"]+)\".*?\n"
+ " url\\s\\+=\\s\"(?<makeURL>.*?)\"\\;.*?\n"
+ " salt=\"(?<salt>[^\"]+)\"";
I don't have any sample data, so it's hard to tell whether it works this way, but it does compile without errors.

Java NewLine "\n" not working in save to text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is my code:
public String display() {
return "\n......................\nFixed Employee:\n" +
"Name: " + super.fullName() +
"\nSalary: " + salary() +
" tk\n......................";
}
But when I'm invoking this method from main class, "\n" newLine not working. just showing one line output. Will you plz help to solve the problem?
Thanks
For saving in files use \r\n. \n as new lines is viable on printstreams but not writing to files.
You may need the system independent line separator as it might differ from one OS to another. Just replace the \n with the value of line separator:
I can be retrieve as you load any system property:
public String display() {
String separator = System.getProperty("line.separator"); // Load the system property using its key.
return "\n......................\nFixed Employee:\n"
+ "Name: "
+ super.fullName() +
"\nSalary: "
+ salary()
+ " tk\n......................"
.replace("\\n", separator); // replace the \n before returning your String
}
Or simply use System#lineSeparator method as #Deepanshu Bedi suggested:
public String display() {
String separator = System.lineSeparator(); // Consider it as a shortcut.
return "\n......................\nFixed Employee:\n"
+ "Name: "
+ super.fullName() +
"\nSalary: "
+ salary()
+ " tk\n......................"
.replace("\\n", separator); // replace the \n before returning your String
}

How to retrieve multiple data (rows) from database SQLite Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is what I have done right now.
My table is like this:
JAPANESEALPHA ENGLISH JAPANESECHAR
kiro kilo \u30ad\u30ed
guramu gram \u30b0\u30e9\u30e0
migi right \u307f\u304e
mijikai short \u307f\u3058\u304b\u3044
Inside database (example):
mDbHelper.createCrv("kiro","kilo","\u30ad\u30ed");
mDbHelper.createCrv("guramu","gram","\u30b0\u30e9\u30e0");
mDbHelper.createCrv("mijikai","short","\u307f\u3058\u304b\u3044");
mDbHelper.createCrv("minami","south","\u307f\u306a\u307f");
mDbHelper.createCrv("miru","watch","\u307f\u308b");
Query:
String query = "SELECT docid as _id," +
KEY_ENGLISH + "," +
KEY_JAPANESEALPHA + "," +
KEY_JAPANESECHAR+
" from " + FTS_VIRTUAL_TABLE +
" where " + KEY_JAPANESEALPHA + " = '" + inputText + "';";
I also have Cursor:
private void showResults(String query) {
Cursor cursor = mDbHelper.searchCrvJapanesechar((query != null ? query.toString() : "####"));
if (cursor == null) {
Toast.makeText(getApplicationContext(),
"No Search Found!", Toast.LENGTH_LONG).show();
} else {
// Specify the columns we want to display in the result
String[] from = new String[] {
DBAdapter.KEY_JAPANESECHAR,
DBAdapter.KEY_JAPANESEALPHA,
DBAdapter.KEY_ENGLISH
};
//DBAdapter.KEY_TAGALOG};
// Specify the Corresponding layout elements where we want the columns to go
int[] to = new int[] { R.id.sjapanesechar,
R.id.sjapanesealpha,
R.id.senglish};
// R.id.stagalog};
For compound words, I need to minimize my database so that when the
Input: kiroguramu
I can concatenate the english term of kiru and english term of guramu. That will result
Result: kilogram
Sorry I can't paste my sample images because I'm newbie.
I have get the retrieving data at http://www.mysamplecode.com/2011/11/android-searchview-using-sqlite-fts3.html It was a great help.
Thankyou.
Hi I think you should try to read some basic of JSON parsing here
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Categories