Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I have a use case where values my function accepts could be of type:-
1. mailto:abc#abc.com
2. tel:3402904323
3. http(s)://www.google.com
4. www.google.com
5. //www.google.com
6. /content/abc/def/index
7. javascript:;
8. blank
9. #
10. http(s)://www.google.com/index.html#abc
11. http(s)://www.google.com/index.html
All these are to be treated as valid and i have to create a URL out of them for e.g. for input 6 i would need to append (.html). For 7th i might need to escapeHtml and rest could be returned as is.
Is there a standard java API to do this or any standard logic i could put into doing this.
Please help.
Use String#matches with the the following regular expressions, respectively.
(?i)mailto:[a-z0-9]{1,}[.a-z0-9]*#[a-z0-9]{1,}[.a-z0-9].[a-z0-9]{2,}
tel:[0-9]{10}
http[s]{0,}://
www\.[a-z0-9\-]{1,}\.com
//www\.[a-z0-9\-]{1,}\.com
/content/([a-z0-9\.\-]{1,}/*)*
nice try
[^\w\W]*
#
http[s]*://[a-z0-9-]{1,}\.[a-z0-9-]{1,}\.com/([a-z0-9-_%\?\=]{1,}/)*[a-zA-Z0-9_-]{1,}\.html#[a-zA-Z_\-\.]{1,}
http[s]*://[a-z0-9-]{1,}\.[a-z0-9-]{1,}\.com/([a-z0-9-_%\?\=]{1,}/)*[a-zA-Z0-9_-]{1,}\.html[a-zA-Z_\-\.]{1,}
Here is an example of using a regular expression, in this case it will return a boolean value (it matches or it doesn't match)
public static void main(String[] args)
{
String pattern = "http[s]*://[a-z0-9-]{1,}\\.[a-z0-9-]{1,}\\.com/([a-z0-9-_%\\?\\=]{1,}/)*[a-zA-Z0-9_-]{1,}\\.html#[a-zA-Z_\\-\\.]{1,}";
String string = "http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#matches-java.lang.String-";
/*
* This will print the boolean literal true or false
*/
System.out.println( string.matches(pattern) );
}
Just try to create a java.net.URI with the string. If there is a syntax error you will get a URISyntaxException. All the above pass provided that "http(s)" really means "http" and "https" separately. If you need to restrict the scheme to what is shown above, just get the scheme and look it up in a table, and fail it if isn't there.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
It is possible to do expression evolution comparison using string in java.
Say if I have list of rules :
create AND [table OR view] AND as AND select
[insert OR delete] AND table
Assume that OR group is always within the square brackets []
I would like do a pattern match (similar to the database GRANT permissions)
I want to block the following commands :
create table something as select * from test => should be blocked (from condition 1)
create view something as select ***** => should be blocked (from condition 1)
insert into table => should be blocked (from condition 2)
delete table => should be blocked (from condition 2)
select * from test => allowed as it doesn't match the rule pattern
In general I want to build something similar to block/grant permission on database queries.
Is there any Java library for expression evaluation matching in Java?
You can make use of the below two simple regex
1.create (table|view).*?as select .*
2.(insert|delete)?.*table
You can test it in https://regex101.com/ and let me know if you find any issue.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I am looking for Opensource Java library which could help me in expanding all the possible values out of Numeric Regex, for example: if I give a range 1234[7-9] as input, it should output 12347,12348, 12349, similarly taking care of 123[4-6][7-9], which would translate to 12347, 12357, 12367 so on. Instead of reinventing wheel I would like to know if there are any libraries which could do this. This is only for Numeric regex with defined range.
I have once tried out Xeger which was good enough for such simple expressions similar to yours above. You will also need automaton jar package, that you can download as a library in order to use Xeger.
Example how to use:
String regex = "123[4-6][7-9]";
Xeger generator = new Xeger(regex);
Set<String> generated = new HashSet<>();
for (int i = 0; i < 100; i++) {
generated.add(generator.generate());
}
System.out.println(generated);
//[12367, 12348, 12359, 12349, 12357, 12368, 12369, 12347, 12358]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am trying to figure out an effective way of getting a random class from my project. There are nine different classes(ten including the startup), each one with a different behavior(which extend a declaratoid class that is used in the main function). I need to be able to have the function to run differently on each startup. What do I need to do? Edit:Thanks for the answer, but I am now running into another problem. I have to pass the result as the first parameter in another the function now.
Create a factory method that gets a random number and creates an object based on that, in a switch:
public static YourInterfaceType createRandom() {
Random r = new Random();
switch(r.nextInt(10)) {
case 1: return new FirstType();
case 2: return new SecondType();
// etc
default: return new LastType();
}
}
Edit More exact definition of words. :)
you could generate a random number from 1 - 9 and use a switch statement where each case calls a different class
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Is there a way in java we can convert an expression (xml or any other) to logical operations.
for example I have a property
prop01=Achivment:APPCom,done&&TODO:getforecast,!done;Achivement done
is there a way I convert it to java code like
Map userData = getUserData();
Map achivements = userData.get("achivements");
Map TODOs = userData.get("TODOs");
String achiv = achivements.get("APPCom");
String todo = TODOs.get("getforecast");
if(achiv == "done" && todo != "done")
system.out.println("Achivement done"); // part after ; in expression
any third party available for this kind of task?
I don't think this can be done with any third party library directly. You may need to use some library like Antlr to write a translator to translate from your expression to your Java code.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this xml file from where I'm reading this string,
http://localhost:8080/sdpapi/request/10/notes/611/
My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?
Split the string
String input = "http://localhost:8080/sdpapi/request/10/notes/611/";
String output = input.split("notes/")[1].split("/")[0];
output is the value you need
What language?
Anyway, in most cases it's a syntax like:
String.substring(begin, length);
... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write
substring(0, 4);
In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of #Liran for example).
// EDIT: In Java the second parameter of substring is not length, but endIndex:
String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));
It depends on programming language you use, but Regular Expressions should be the same in most of them:
/(\d+)\/$/
well, it depend in what language are you writing... in c# for example
string s = #"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));
or
Path.GetFileName(s);
for java
new File(s).getName();