regular expression - parse classpath location - java

$JAR_REPO/nlb/grbox/smnt.jar
I want to get the string between $ and first / and this will be replaced with some other string.
What is the regex to get JAR_REPO alone from above?
Can I use Regex to get the actual string like the pattern match (any method) will return the string JAR_REPO?
Please help.
Thanks.
Wells

\$([^/]+)/.*
or, as a Java String:
"\\$([^/]+)/.*"
The JAR_REPO String will be the group(1):
Pattern pattern = Pattern.compile("\\$([^/]+)/.*");
Matcher matcher = pattern.matcher(yourstring);
if (matcher.find()) {
String jarRepo = matcher.group(1);
}

Such type of recursive parse approach can be resolved using Interpreter Pattern logic along with parsing approach.

Related

How to extract a substring using regex for this pattern

i have to extract a string between / and ?, i.e exampleproduct
https://local.host.com/order/faces/Home/myorder/exampleproduct?_adf.ctrl-state=mfun9p14r_19
how to write regular expression for this
i am using this logic but i am unable to
private static String extractPageNameFromURL(String urlFull) {
if (urlFull != null) {
Pattern pattern = Pattern.compile("/(.*?).jspx?");
Matcher matcher = pattern.matcher(urlFull);
while (matcher.find()) {
String str1 = matcher.group(1);
String[] dataRows = str1.split("/");
urlFull = dataRows[dataRows.length - 1];
}
}
return urlFull;
}
public static void main(String[] args) {
System.out.println(DtmUtils.extractPageNameFromURL("https://local.host.com/order/faces/Home/myorder/exampleproduct?_adf.ctrl-state=mfun9p14r_19"));
}
Thanks
Raj
If I'm following what you're asking, then you're attempting to pull exampleproduct from the URL.
Here's the regex to use to accomplish this. Group 1 should have the name after the last / and before the first ? after that slash.
^.*\/([^?]+)\?.*$
See an example of the regex
^ -- Beginning of line anchor
.* -- find 0 or more characters. Note the * is greedy.
\/ -- Find a literal /.
([^?]+) -- Capture everything that is not a question mark
\? -- Find a literal question mark
.* -- now match everything after the question mark
$ -- end of line anchor
and here's a quick example of using it in Java. This is a quick example, and will need to be modified before using it.
String urlFull = "https://local.host.com/order/faces/Home/myorder/exampleproduct?_adf.ctrl-state=mfun9p14r_19";
Pattern pattern = Pattern.compile("^.*\\/([^?]+)\\?.*$");
Matcher matcher = pattern.matcher(urlFull);
matcher.find();
String p = matcher.group(1);
System.out.println(p);
I didn't follow why the original regex you wrote had the .jspx?, but if there's more to the problem you'll need to update the question to explain.
To match exampleproduct in your input this lookahead based regex will work for you:
[^/]+(?=\?)
In Java code:
Pattern pat = Pattern.compile("[^/]+(?=\\?)");
RegEx Demo
This pattern might work for you \?(.*).
This regex finds a question mark and selects everything after it.
I tried this pattern with your path and it worked fine: ([\w_-]*)(\.)*([\w_-]*)?(?=\?)
It would also match, if your filename had a file ending.

Silly RegEx issue. What am I doing wrong?

String url = "hello world";
String p = "world";
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(url);
if (matcher.matches()) {
int start = matcher.start();
int end = matcher.end();
}
What am I doing wrong? How comes the if statement never gets hit?
The matches() method attempts to match the entire string to the pattern. You want the find() method.
Try Matcher.find(). Matcher.matches() checks whether the whole string matches the pattern.
You need to use find because,
matches tries to match the patten against the entire string and
implicitly add a ^ at the start and $ at the end of your pattern.
So your pattern is equivalent to "^world$".
Try to change your pattern to ".*world.*":
String p = ".*world.*";
That way it'll match any string that contains "world".
I experienced same problem.
I don't know the reason.
If someone knows problem please post here.
I had solved problem with using find() repeatedly instead of matches().

java regular expressions matching specific urls

I once built a program in php that used very specific regular expressions to match links, however that pattern doesnt seem to work in java, Im trying to find the java equivalent of
"~http://(bit.ly|t.co)~"
in php this would would match links such as http://t.co/UURRNlrK and http://bit.ly/AenG5W what would be a java equivalent of this?
I think you are looking for
String str = "http://t.co/UURRNlrK";
String p = "(http://(t\.co|bit\.ly).*)";
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(str);
if(matcher.find())
System.out.println(matcher.group(0));
Output = http://t.co/UURRNlrK
if str = "http://bit.ly/AenG5W"
Output = http://bit.ly/AenG5W
Here is a nice Regex Tutorial for java.
http://(bit\.ly|t\.co)/\w*
I think this one would result same as the upper ones
I tried this:
String str = "http://bit.ly/asdfsd";
if(str.matches("http://(bit\.ly|t\.co).+")){
System.out.println("hurray");
}

Java regex validating special chars

This seems like a well known title, but I am really facing a problem in this.
Here is what I have and what I've done so far.
I have validate input string, these chars are not allowed :
&%$###!~
So I coded it like this:
String REGEX = "^[&%$###!~]";
String username= "jhgjhgjh.#";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(username);
if (matcher.matches()) {
System.out.println("matched");
}
Change your first line of code like this
String REGEX = "[^&%$##!~]*";
And it should work fine. ^ outside the character class denotes start of line. ^ inside a character class [] means a negation of the characters inside the character class. And, if you don't want to match empty usernames, then use this regex
String REGEX = "[^&%$##!~]+";
i think you want this:
[^&%$###!~]*
To match a valid input:
String REGEX = "[^&%$##!~]*";
To match an invalid input:
String REGEX = ".*[&%$##!~]+.*";

extract substring in java using regex

I need to extract "URPlus1_S2_3" from the string:
"Last one: http://abc.imp/Basic2#URPlus1_S2_3,"
using regular expression in Java language.
Can someone please help me? I am using regex for the first time.
Try
Pattern p = Pattern.compile("#([^,]*)");
Matcher m = p.matcher(myString);
if (m.find()) {
doSomethingWith(m.group(1)); // The matched substring
}
String s = "Last one: http://abc.imp/Basic2#URPlus1_S2_3,";
Matcher m = Pattern.compile("(URPlus1_S2_3)").matcher(s);
if (m.find()) System.out.println(m.group(1));
You gotta learn how to specify your requirements ;)
You haven't really defined what criteria you need to use to find that string, but here is one way to approach based on '#' separator. You can adjust the regex as necessary.
expr: .*#([^,]*)
extract: \1
Go here for syntax documentation:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
String s = Last one: http://abc.imp/Basic2#URPlus1_S2_3,"
String result = s.replaceAll(".*#", "");
The above returns the full String in case there's no "#". There are better ways using regex, but the best solution here is using no regex. There are classes URL and URI doing the job.
Since it's the first time you use regular expressions I would suggest going another way, which is more understandable for now (until you master regular expressions ;) and it will be easily modified if you will ever need to:
String yourPart = new String().split("#")[1];
Here's a long version:
String url = "http://abc.imp/Basic2#URPlus1_S2_3,";
String anchor = null;
String ps = "#(.+),";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(url);
if (m.matches()) {
anchor = m.group(1);
}
The main point to understand is the use of the parenthesis, they are used to create groups which can be extracted from a pattern. In the Matcher object, the group method will return them in order starting at index 1, while the full match is returned by the index 0.
If you just want everything after the #, use split:
String s = "Last one: http://abc.imp/Basic2#URPlus1_S2_3," ;
System.out.println(s.split("#")[1]);
Alternatively, if you want to parse the URI and get the fragment component you can do:
URI u = new URI("http://abc.imp/Basic2#URPlus1_S2_3,");
System.out.println(u.getFragment());

Categories

Resources