search for string in line - java

I have problem dealing with a string in a line,
For example i have the following lines in a .txt file:
ussdserver link from host /127.0.0.1:38978(account smpp34) is up|smpp34|2012-10-28 17:02:19
ussdserver link from host localhost/127.0.0.1:8088(account callme) is up|callme|2012-10-28 17:02:20
I need my code to get the word after "account" (in the first line it's smpp34) and the word "up" (after the "is" word).
I thought about using String.charAt() method but it doesn't work here because the words that I need can be in different places, as shown in the example above.

Try using following methods form String class.
String inputStr = "ussdserver link from host /127.0.0.1:38978(account smpp34) is up|smpp34|2012-10-28 17:02:19";
int index1 = inputStr.indexOf("account");
int index2 = inputStr.indexOf(')',index1);
String accountName = inputStr.substring(index1+8,index2); // you get smpp34
index1 = inputStr.indexOf("|");
index2 = inputStr.lastIndexOf(' ', index1);
String state = inputStr.substring(index2+1, index1) // you get up

Yeah its best to use regex in this sort of cases..But a simpler method can be used specifically for above case. Simply Try splitting from ) and then read till length of string-5 to length will give you the first word and try similar for second word..
IF and Only if the String pattern above never changes..Else will recommend using regex..

Try RegEx like this.
Pattern pattern = Pattern.compile(".*account\\s([^\\s]*)\\)\\sis\\s([^|]*)|.*");
Matcher matcher = pattern.matcher("ussdserver link from host /127.0.0.1:38978(account smpp34) is up|smpp34|2012-10-28 17:02:19");
while (matcher.find()) {
System.out.println(matcher.group(1));//will give you 'smpp34'
System.out.println(matcher.group(2));//will give you 'up'
return;
}

Related

Split a string based on pattern and merge it back

I need to split a string based on a pattern and again i need to merge it back on a portion of string.
for ex: Below is the actual and expected strings.
String actualstr="abc.def.ghi.jkl.mno";
String expectedstr="abc.mno";
When i use below, i can store in a Array and iterate over to get it back. Is there anyway it can be done simple and efficient than below.
String[] splited = actualstr.split("[\\.\\.\\.\\.\\.\\s]+");
Though i can acess the string based on index, is there any other way to do this easily. Please advise.
You do not understand how regexes work.
Here is your regex without the escapes: [\.\.\.\.\.\s]+
You have a character class ([]). Which means there is no reason to have more than one . in it. You also don't need to escape .s in a char class.
Here is an equivalent regex to your regex: [.\s]+. As a Java String that's: "[.\\s]+".
You can do .split("regex") on your string to get an array. It's very simple to get a solution from that point.
I would use a replaceAll in this case
String actualstr="abc.def.ghi.jkl.mno";
String str = actualstr.replaceAll("\\..*\\.", ".");
This will replace everything with the first and last . with a .
You could also use split
String[] parts = actualString.split("\\.");
string str = parts[0]+"."+parts[parts.length-1]; // first and last word
public static String merge(String string, String delimiter, int... partnumbers)
{
String[] parts = string.split(delimiter);
String result = "";
for ( int x = 0 ; x < partnumbers.length ; x ++ )
{
result += result.length() > 0 ? delimiter.replaceAll("\\\\","") : "";
result += parts[partnumbers[x]];
}
return result;
}
and then use it like:
merge("abc.def.ghi.jkl.mno", "\\.", 0, 4);
I would do it this way
Pattern pattern = Pattern.compile("(\\w*\\.).*\\.(\\w*)");
Matcher matcher = pattern.matcher("abc.def.ghi.jkl.mno");
if (matcher.matches()) {
System.out.println(matcher.group(1) + matcher.group(2));
}
If you can cache the result of
Pattern.compile("(\\w*\\.).*\\.(\\w*)")
and reuse "pattern" all over again this code will be very efficient as pattern compilation is the most expensive. java.lang.String.split() method that other answers suggest uses same Pattern.compile() internally if the pattern length is greater then 1. Meaning that it will do this expensive operation of Pattern compilation on each invocation of the method. See java.util.regex - importance of Pattern.compile()?. So it is much better to have the Pattern compiled and cached and reused.
matcher.group(1) refers to the first group of () which is "(\w*\.)"
matcher.group(2) refers to the second one which is "(\w*)"
even though we don't use it here but just to note that group(0) is the match for the whole regex.

Look for a certain String inside another and count how many times it appears

I am trying to search for a String inside a file content which I got into a String.
I've tried to use Pattern and Matcher, which worked for this case:
Pattern p = Pattern.compile("(</machine>)");
Matcher m = p.matcher(text);
while(m.find()) //if the text "(</machine>)" was found, enter
{
Counter++;
}
return Counter;
Then, I tried to use the same code to find how many tags I have:
Pattern tagsP = Pattern.compile("(</");
Matcher tagsM = tagsP.matcher(text);
while(tagsM.find()) //if the text "(</" was found, enter
{
CounterTags++;
}
return CounterTags;
which in this case, the return value was always 0.
Try using the below code , btw not using Pattern:-
String actualString = "hello hi how(</machine>) are you doing. Again hi (</machine>) friend (</machine>) hope you are (</machine>)doing good.";
//actualString which you get from file content
String toMatch = Pattern.quote("(</machine>)");// for coverting to regex literal
int count = actualString .split(toMatch, -1).length - 1; // split the actualString to array based on toMatch , so final match count should be -1 than array length.
System.out.println(count);
Output :- 4
You can use Apache commons-lang util library, there is a function countMatches exactly for you:
int count = StringUtils.countMatches(text, "substring");
Also this function is null-safe.
I recommend you to explore Apache commons libraries, they provide a lot of useful common util methods.

Java String- How to get a part of package name in android?

Its basically about getting string value between two characters. SO has many questions related to this. Like:
How to get a part of a string in java?
How to get a string between two characters?
Extract string between two strings in java
and more.
But I felt it quiet confusing while dealing with multiple dots in the string and getting the value between certain two dots.
I have got the package name as :
au.com.newline.myact
I need to get the value between "com." and the next "dot(.)". In this case "newline". I tried
Pattern pattern = Pattern.compile("com.(.*).");
Matcher matcher = pattern.matcher(beforeTask);
while (matcher.find()) {
int ct = matcher.group();
I tried using substrings and IndexOf also. But couldn't get the intended answer. Because the package name in android varies by different number of dots and characters, I cannot use fixed index. Please suggest any idea.
As you probably know (based on .* part in your regex) dot . is special character in regular expressions representing any character (except line separators). So to actually make dot represent only dot you need to escape it. To do so you can place \ before it, or place it inside character class [.].
Also to get only part from parenthesis (.*) you need to select it with proper group index which in your case is 1.
So try with
String beforeTask = "au.com.newline.myact";
Pattern pattern = Pattern.compile("com[.](.*)[.]");
Matcher matcher = pattern.matcher(beforeTask);
while (matcher.find()) {
String ct = matcher.group(1);//remember that regex finds Strings, not int
System.out.println(ct);
}
Output: newline
If you want to get only one element before next . then you need to change greedy behaviour of * quantifier in .* to reluctant by adding ? after it like
Pattern pattern = Pattern.compile("com[.](.*?)[.]");
// ^
Another approach is instead of .* accepting only non-dot characters. They can be represented by negated character class: [^.]*
Pattern pattern = Pattern.compile("com[.]([^.]*)[.]");
If you don't want to use regex you can simply use indexOf method to locate positions of com. and next . after it. Then you can simply substring what you want.
String beforeTask = "au.com.newline.myact.modelact";
int start = beforeTask.indexOf("com.") + 4; // +4 since we also want to skip 'com.' part
int end = beforeTask.indexOf(".", start); //find next `.` after start index
String resutl = beforeTask.substring(start, end);
System.out.println(resutl);
You can use reflections to get the name of any class. For example:
If I have a class Runner in com.some.package and I can run
Runner.class.toString() // string is "com.some.package.Runner"
to get the full name of the class which happens to have a package name inside.
TO get something after 'com' you can use Runner.class.toString().split(".") and then iterate over the returned array with boolean flag
All you have to do is split the strings by "." and then iterate through them until you find one that equals "com". The next string in the array will be what you want.
So your code would look something like:
String[] parts = packageName.split("\\.");
int i = 0;
for(String part : parts) {
if(part.equals("com")
break;
}
++i;
}
String result = parts[i+1];
private String getStringAfterComDot(String packageName) {
String strArr[] = packageName.split("\\.");
for(int i=0; i<strArr.length; i++){
if(strArr[i].equals("com"))
return strArr[i+1];
}
return "";
}
I have done heaps of projects before dealing with websites scraping and I
just have to create my own function/utils to get the job done. Regex might
be an overkill sometimes if you just want to extract a substring from
a given string like the one you have. Below is the function I normally
use to do this kind of task.
private String GetValueFromText(String sText, String sBefore, String sAfter)
{
String sRetValue = "";
int nPos = sText.indexOf(sBefore);
if ( nPos > -1 )
{
int nLast = sText.indexOf(sAfter,nPos+sBefore.length()+1);
if ( nLast > -1)
{
sRetValue = sText.substring(nPos+sBefore.length(),nLast);
}
}
return sRetValue;
}
To use it just do the following:
String sValue = GetValueFromText("au.com.newline.myact", ".com.", ".");

How to copy a certain word from one string to another in Java

I need to take a String that has the following data for example: hello (world). My plan is, I will have the program add individual letters to another string, and test that second string and see if it has (. Then, if it does, it will start adding the letters to a third string until it finds ). That is my idea about how to get whatever is in the parentheses over to the other String. The problem is, I have no idea how to do it. I'm still kind of new to Java, and I don't know a lot of the more advanced things you can do with Strings. So if anyone has any ideas, or a tutorial explaining some code that would help me, it would be greatly appreciated.
This can be done using the indexOf() and substring() methods in the String class.
String s = "Hello (world)";
String s2 = s.substring(s.indexOf('(')+1, s.indexOf(')'));
The value of s2 should now be world.
String.indexOf("(");
Would return the index of the first occurence of a (.
If the data you have are always looking like that, you can then use substring() to recieve the wanted string, since the data ends with ).
String s = "hello (world)";
String wanted = s.substring(s.indexOf("("), s.length);
should to the trick.
You can find documentation on the String class here. That should be enough to help you figure out what is available, but if you want some helper methods, consider adding using Apache Common Lang StringUtils.
Might want to use regex:
String myString = "Hello (world)";
Pattern pattern = Pattern.compile("\\((.*)\\)");
Matcher matcher = pattern.matcher(myString);
if (matcher.find()) {
String newString = matcher.group(1);
System.out.println(newString);
}
This will print "world".
You can get many example on Internet on how to use Regex in Java: http://www.tutorialspoint.com/java/java_regular_expressions.htm
Make use of String.indexOf(int ch, int fromIndex) and String.substring(int beginIndex, int endIndex) function.
read the first index using int i = string.indexOf("(") and then using this index to find the ) parenthesis:
int i = string.indexOf("(");
int j = string.indexof(")", i+1);
String target = string.substring(i+1, j);

Get word part from a variable String

I've been implementing an application to retrieve a word inside a incoming String parameter, this String parameter can vary since it is an URL, but the pattern for almost all the incoming url's is the same. For instance I could have:
GET /com.myapplication.v4.ws.monitoring.ModuleSystemMonitor HTTP/1.1
or
GET /com.myapplication.filesystem.ws.ModuleFileSystem/getIdFolders/jsonp?idFolder=idFis1&callback=__gwt_jsonp__.P0.onSuccess&failureCallback=__gwt_jsonp__.P0.onFailure HTTP/1.1
So in any case, I want to extract the word that starts with Module, for example, for the first incoming parameter I want to get: ModuleSystemMonitor. And for the second one I want to get the word: ModuleFileSystem.
This is the requirement, I'm not allowed to do anything else but this: just a method that receives a line and try to extract the words I mentioned: ModuleSystemMonitor and ModuleFileSystem.
I've been thinkng of using StringTokenizer class or String#split method, but I'm not sure if they are the best option. I tried and it is easy to get the word begins with Module using indexOf, but how to cut the word if from some cases it comes with a white space like the first sample or it comes with a "/" (slash) in the second. I know I can make an "if" statement and cut it when it is white space or it is slash but I wonder to know if there is another way that could be more dynamic.
Thanks in advance for your time and help. Best regards.
I'm not sure this is the best solution but you could try this:
String[] tmp = yourString.Split("\\.|/| ");
for (int i=0; i< tmp.length(); i++) {
if (tmp[i].matches("^Module.*")) {
return tmp[i];
}
}
return null;
You can just use String.indexOf and String.substring like this:
int startIndex = url.indexOf("Module");
for (int index = startIndex + "Module".length; i < url.length; i++
{
if (!Character.isLetter(url.charAt(index))
{
return url.substring(startIndex, index));
}
}
Based on the assumption that the first non-letter character is the end marker of the word.
String stringToSearch = "GET /com.myapplication.v4.ws.monitoring.ModuleSystemMonitor HTTP/1.1";
Pattern pattern = Pattern.compile("(Module[a-zA-Z]*)");
Matcher matcher = pattern.matcher(stringToSearch);
if (matcher.find()){
System.out.println(matcher.group(1));
}

Categories