Extracting only text form String in Java - java

I have got string like str = Adobe Flash Player 11.4.402.287 (11.3 MB), I need to extract only Adobe Flash Player as the output. Pls suggest..
I tried using Regex like :
String str = "Adobe Flash Player 11.4.402.287 (11.3 MB)";
Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group(1));
}

As suggested by #MarkoTopolink, regexp [\\p{L}\\s]+ helped me. thanks.

Try this:
String str = "Adobe Flash Player 11.4.402.287 (11.3 MB)";
Pattern p = Pattern.compile("^([a-zA-Z ]+)([0-9]+).*");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group(1));
}
There are two problems in your try:
Grouping is done using (), you did not define a group for the text you actually wanted
You need to add a space to get more than one word.

You can use regex:
String str = "Adobe Flash Player 11.4.402.287 (11.3 MB)";
String [] strs = str.split("([ |0-9|.|(.*MB)]*) [ |0-9|.|(.*MB)]*");
for (String strng : strs) {
System.out.println(strng.trim());
}

Related

method to take string inside curly braces using split or tokenizer

String s = "author= {insert text here},";
Trying to get the inside of the string, ive looked around but couldn't find a resolution with just split or tokenizer...
so far im doing this
arraySplitBracket = s.trim().split("\\{", 0);
which gives me insert text here},
at array[1] but id like a way to not have } attached
also tried
StringTokenizer st = new StringTokenizer(s, "\\{,\\},");
But it gave me author= as output.
public static void main(String[] args) {
String input="{a c df sdf TDUS^&%^7 }";
String regEx="(.*[{]{1})(.*)([}]{1})";
Matcher matcher = Pattern.compile(regEx).matcher(input);
if(matcher.matches()) {
System.out.println(matcher.group(2));
}
}
You can use \\{([^}]*)\\} Regex to get string between curly braces.
Code Snap :
String str = "{insert text here}";
Pattern p = Pattern.compile("\\{([^}]*)\\}");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
}
Output :
insert text here
String s = "auther ={some text here},";
s = s.substring(s.indexOf("{") + 1); //some text here},
s = s.substring(0, s.indexOf("}"));//some text here
System.out.println(s);
How about taking a substring by excluding the character at arraySplitBracket.length()-1
Something like
arraySplitBracket[1] = arraySplitBracket[1].substring(0,arraySplitBracket.length()-1);
Or use String Class's replaceAll function to replace } ?

How to extract id from url ? Google sheet

I have the follow urls.
https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258
https://docs.google.com/a/example.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY/edit#gid=1842172258
https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY
Foreach url, I need to extract the sheet id: 1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY into a java String.
I am thinking of using split but it can't work with all test cases:
String string = "https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258";
String[] parts = string.split("/");
String res = parts[parts.length-2];
Log.d("hello res",res );
How can I that be possible?
You can use regex \/d\/(.*?)(\/|$) (regex demo) to solve your problem, if you look closer you can see that the ID exist between d/ and / or end of line for that you can get every thing between this, check this code demo :
String[] urls = new String[]{
"https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258",
"https://docs.google.com/a/example.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY/edit#gid=1842172258",
"https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY"
};
String regex = "\\/d\\/(.*?)(\\/|$)";
Pattern pattern = Pattern.compile(regex);
for (String url : urls) {
Matcher matcher = pattern.matcher(url);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
Outputs
1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY
1mrsetjgfZI2BIypz7SGHMOfHGv6PTKTzY0xOM5c6TXY
1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY
it looks like the id you are looking for always follow "/spreadsheets/d/" if it is the case you can update your code to that
String string = "https://docs.google.com/spreadsheets/d/1mrsetjgfZI2BIypz7SGHMOfHGv6kTKTzY0xOM5c6TXY/edit#gid=1842172258";
String[] parts = string.split("spreadsheets/d/");
String result;
if(parts[1].contains("/")){
String[] parts2 = parts[1].split("/");
result = parts2[0];
}
else{
result=parts[1];
}
System.out.println("hello "+ result);
Using regex
Pattern pattern = Pattern.compile("(?<=\\/d\\/)[^\\/]*");
Matcher matcher = pattern.matcher(url);
System.out.println(matcher.group(1));
Using Java
String result = url.substring(url.indexOf("/d/") + 3);
int slash = result.indexOf("/");
result = slash == -1 ? result
: result.substring(0, slash);
System.out.println(result);
Google use fixed lenght characters for its IDs, in your case they are 44 characters and these are the characters google use: alphanumeric, -, and _ so you can use this regex:
regex = "([\w-]){44}"
match = re.search(regex,url)

Extract json data from given string

I am having a string something like this :
a.b.c.d.e =
{"altImages":2,"available":1,"availableColorCount":3};
Now I only need to fetch :
{"altImages":2,"available":1,"availableColorCount":3}
What should be regex expression to extract that part from given string. Please help
My Try :
(?smi)a.b.c.d\\(.*\"e\"=(.*?)\\}\\);.*
But its not helping around.
Try this:
.+\s*=\s*({(?:.+:.+,?)+})(?=;)
You can use something like:
.*?\n(.*);
Here is the version with named groups:
String text = "a.b.c.d.e = \n{\"altImages\":2,\"available\":1,\"availableColorCount\":3};";
Pattern pattern = Pattern.compile(".*?\n(?<JSON>.*);");
Matcher matcher = pattern.matcher(text);
if (matcher.matches()) {
System.out.println(matcher.group("JSON"));
}

Regex - find data inside left and right encloses

I have this string:
text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz
I need to extract 123+456+789
What I done so far is:
String s = "text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz";
String ps = "text=(.*)&";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(s);
if (m.find()){
System.out.println(m.group(0));
System.out.println(m.group(1));
}
And I got all text until the last & which is: 123+456+789&xxxxxxxxx&yyyyyyyyyy while the requested output is: 123+456+789
Any suggestions how to fix it (regex is mandatory)?
Use a negated character class:
String ps = "text=([^&]*)";
The value you need will be in Group 1.
The [^&] matches any character but an ampersand.
You almost getting, you need to make your regex lazy (or non greedy) like this:
String ps = "text=(.*?)&";
here ---^
Working demo
Try this regex :
([0-9+]+)
Link : https://regex101.com/r/xU2zF4/1
java code :
String s = "text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz";
String ps = "([0-9+]+)";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(s);
if (m.find()){
System.out.println(m.group(0)); // value of s
System.out.println(m.group(1)); // returns 123+456+789
}

Extracting string from within round brackets in Java with regex

I'm trying to extract a string from round brackets.
Let's say, I have John Doe (123456789) and I want to output the string 123456789 only.
I have found this link and this regex:
/\(([^)]+)\)/g
However, I wasn't able to figure out how to get the wanted result.
Any help would be appreciated. Thanks!
String str="John Doe (123456789)";
System.out.println(str.substring(str.indexOf("(")+1,str.indexOf(")")));
Here I'm performing string operations. I'm not that much familiar with regex.
this works for me :
#Test
public void myTest() {
String test = "test (mytest)";
Pattern p = Pattern.compile("\\((.*?)\\)");
Matcher m = p.matcher(test);
while(m.find()) {
assertEquals("mytest", m.group(1));
}
}
You need to escape brackets in your regexp:
String in = "John Doe (123456789)";
Pattern p = Pattern.compile("\\((\\d*)\\)");
Matcher m = p.matcher(in);
while (m.find()) {
System.out.println(m.group(1));
}
In Java, you need to use
String pattern = "\\(([^()]+)\\)";
Then, the value you need is in .group(1).
String str = "John Doe (123456789)";
String rx = "\\(([^()]+)\\)";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
}
See IDEONE demo

Categories