How can I extract the value of the bookid from this string using Java?
href="http://www.books.com/?bookname=cooking&bookid=12345678&bookprice=123.45">cooking book
Normally you should use some parser but if your String is really this short then maybe regular expression can be option like
String s="href=\"http://www.books.com/?bookname=cooking&bookid=12345678&bookprice=123.45\">cooking book";
Pattern p= Pattern.compile("(?<=bookid=)\\d+");
Matcher m=p.matcher(s);
if (m.find())
System.out.println(m.group());
output:
12345678
A very simple answer would be
String[] queryParams = url.split("\\?")[1].split("&");
This would give you all the parameters in a=b form in each of the element. You can then just split the needed param.
But ideally you should extract the value by param name
Pshemo you beat me to it but you can also use this:
"(id\=[0-9]*)"
and try RegexPlanet to try out your regex and retrieve the escapped string in java format
You can use the following code snippet:
String str = "href=\"http://www.books.com/?bookname=cooking&bookid=12345678&bookprice=123.45\">cooking book";
String[] strArray = str.split("&");
String bookId = "";
for(int i=0;i<strArray.length;i++)
{
if(strArray[i].startsWith("bookid"))
{
bookId = strArray[i].split("=")[1];
}
}
System.out.println("Book ID = "+bookId);
Related
I am trying to get a regex to match, then get the value with it. For example, I want to check for 1234 as an id and if present, get the status (which is 0 in this case). Basically its id:status. Here is what I am trying:
String topicStatus = "1234:0,567:1,89:2";
String someId = "1234";
String regex = "\\b"+someId+":[0-2]\\b";
if (topicStatus.matches(regex)) {
//How to get status?
}
Not only do I not know how to get the status without splitting and looping through, I don't know why it doesn't match the regex.
Any help would be appreciated. Thanks.
Use the Pattern class
String topicStatus = "1234:0,567:1,89:2";
String someId = "1234";
String regex = "\\b"+someId+":[0-2]\\b";
Pattern MY_PATTERN = Pattern.compile(regex);
Matcher m = MY_PATTERN.matcher(topicStatus);
while (m.find()) {
String s = m.group(1);
System.out.println(s);
}
The key here is to surround the position you want [0-2] in parenthesis which means it will be saved as the first group. You then access it through group(1)
I made some assumptions that your pairs we're always comma separate and then delimited by a colon. Using that I just used split.
String[] idsToCheck = topicStatus.split(",");
for(String idPair : idsToCheck)
{
String[] idPairArray = idPair.split(":");
if(idPairArray[0].equals(someId))
{
System.out.println("id : " + idPairArray[0]);
System.out.println("status: " + idPairArray[1]);
}
}
I need help in splitting two email address which are seperated by a Delimiter 'AND'. I have issue when splitting, when the email address has got the characters'AND' in the email id. For eg, if the email address that needs to be split is something like the below. There are no whitespaces between the two email address.
'anandc#AND.comANDxyz#yahoo.co.in', and the delimiter is'AND'
In the above case, there seems to be three items extracted instead of two. Can someone please help me solve this. Thanks in Advance
You can use " AND " as delimiter.
String str="anandc#AND.com AND xyz#yahoo.co.in";
String[] emailArr=str.split(" AND ");
Or you can use following regex
String str = "anandc#AND.com AND xyz#yahoo.co.in";
Pattern p = Pattern.compile("[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9]+
(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})");
Matcher matcher = p.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
Out put
anandc#AND.com
xyz#yahoo.co.in
Giving correct output
public class Test {
public static void main(String args[]) {
String text = "anandc#AND.com AND xyz#yahoo.co.in ";
String[] splits = text.split(" AND ");
for (int i = 0; i < splits.length; i++) {
System.out.println("data :" + splits[i]);
}
}
}
Output is
data :anandc#AND.com
data :xyz#yahoo.co.in
Use this :
String[] splits = text.split("\\s+AND\\s+");
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
the regular expression will be case sensitive
actually, the best is to use delimiters exression that you are sure will not be in the adress
hi I have a string like this:
String s = "#Path(\"/BankDBRestService/customerExist(String")\";
I want to make that string as
#Path("/BankDBRestService/customerExist")
I tried this:
String foo = s.replaceAll("[(](.*)", "");
i am getting output as
#Path
can anyone please provide me the reqular expression to make this work
Try this one :
String s = "#Path(\"/BankDBRestService/customerExist(String\")";
String res = s.replaceAll("[(]([a-zA-Z_])+", "");
System.out.println(res);
Output : #Path("/BankDBRestService/customerExist")
If your string is "#Path(\"/BankDBRestService/customerExist(String)\")" i.e. with closed parenthesis then use regex [(]([a-zA-Z_])+[)]
try this
String foo = s.replaceAll("(.*)\\(.*\\)", "$1");
Is there a more efficient way of splitting a string than this?
String input = "=example>";
String[] split = input.split("=");
String[] split1 = split[1].split(">");
String result = split1[0];
The result would be "example".
String result = input.replaceAll("[=>]", "");
Very simple regex!
To learn more, go to this link: here
Do you really need regex. You can do:
String result = input.substring(1, input.length()-1);
Otherwise if you really have a case for regex then use character class:
String result = input.replaceAll("[=>]", "");
If you just want to get example out of that do this:
input.substring(1, input.lastIndexOf(">"))
If the string of yours defenitely constant format use substring otherwise go fo regex
result = result.substring(1, result.length() - 1);
You can do it more elegant with RegEx groups:
String sourceString = "=example>";
// When matching, we can "mark" a part of the matched pattern with parentheses...
String patternString = "=(.*?)>";
Pattern p = Pattern.compile(patternString);
Matcher m = p.matcher(sourceString);
m.find();
// ... and access it later
String result = m.group(1);
You can try this regex: ".*?((?:[a-z][a-z]+))"
But it would be better when you use something like this:
String result = input.substring(1, input.length()-1);
try this
String result = input.replace("[\\W]", "")
You can try this too
String input = "=example>";
System.out.println(input.replaceAll("[^\\p{L}\\p{Nd}]", ""));
This will remove all non-words characters
Regex would do the job perfectly, but just to add something new for future solutions you also could use a third party lib such as Guava from Google, it adds a lot of functionalities to your project and the Splitter is really helpful to solve something like you have.
How can I extract the "id" from the following string using regex.
string = 11,"col=""book"" id=""title"" length=""10""
I need to be able to extract the "id" header along with the value "title".
outcome: id=""title""
I am trying to the use split function with a regex to extract the identifier from the string.
Try this:
String result = "col=\"book\" id=\"title\" length=\"10\"";
String pattern = ".*(id\\s*=\\s*\"[^\"]*\").*";
System.out.println(result.replaceAll(pattern,"$1"));
Cheers!
Use Pattern and Matcher classes to find what you are looking for. Try to find these regex \\bid=[^ ]*.
String data = "string = 11,\"col=\"\"book\"\" id=\"\"title\"\" length=\"\"10\"\"";
Matcher m = Pattern.compile("\\bid=[^ ]*").matcher(data);
if (m.find())
System.out.println(m.group());