Match path with or without url parameters - java

I am trying to write a simple regex to match the following:
/path/foo.html
/path/foo.html?a=b
/path/foo.html?a=b&b=c
but not /path/foo.htmlx or anything else which is not foo.html + url parameter.
I tried
/path/foo.html(?:\?|$)
but it does not seem to work in my java project.

String.matches(String regex) does a full match
Tells whether or not this string matches the given regular expression.
An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression
So for example "/path/foo.html?a=b".matches("/path/foo.html(?:\\?|$)") returns false, because the String doesn't end after the ?.
You can use "/path/foo.html(\\?.*)?"

For URL part use regexp provided by #reconnect:
^[^?]*
match from start until ? character meet.
for search exactly /path/foo.html use:
^/path/foo.html
if you need check that part of something exists with matches in the line add ^ - start of line and $ end of line, and in this case you should care about characters between end of your conditions, basically replaced with .*

I'm not exactly understand what you trying to match, but if you want match only URL part without search string:
/^[^?#]*/
# - hash string, too can pass some arguments

Related

Regular expression to return results that do not match selection

I work on a product that provides a Java API to extend it.
The API provides a function which
takes a Perl regular expression and
returns a list of matching files.
I want to filter the list to remove all files that end in .xml, .xsl and .cfg; basically the opposite of .*(\.xml|\.xsl|\.cfg).
I have been searching but I haven't been able to get anything to work yet.
I tried .*(?!\.cfg) and ^((?!cfg).)*$ and \.(?!cfg$|?!xml$|?!xsl$).
I don't know if I am on the right track or not.
Note
I know the regex systems are similar, but I can't get a Java regex working either.
You may use
^(?!.*\.(x[ms]l|cfg)$).+
See the regex demo
Details:
^ - start of a string
(?!.*\.(x[ms]l|cfg)$) - a negative lookahead that fails the match if any 0+ chars other than line break chars (.*) are followed with xml, xsl or cfg ((x[ms]l|cfg)) at the end of the string ($)
.+ - any 1 or more chars other than linebreak chars. Might be omitted if the entire string match is not required (in some tools it is required though).
You need something like this, which matches only if the end of the string isn't preceded by a dot and one of the three unwanted types
/(?<!\.(?:xml|xsl|cfg))\z/

Regular Expression allowing special characters

I am using the following pattern to match my String:
[a-zA-Z0-9]*
Even when I am passing the String *$#, its getting matched by the regular expression. Could someone explain what am I doing wrong or why this is happening?
You should use ^(start of the string) and $(end of the string).
So,the regex would be
^[a-zA-Z0-9]*$
[a-zA-Z0-9]* would match anywhere in the string if you use find method..Using ^ and $ would match the entire input from start till end
if you use matches method you don't need to have ^,$ as it tries to match the entire string
[a-zA-Z0-9]* means 0 or more of any of those characters. If you're using Matcher.find(), it'll find that anywhere/everywhere because it can match anywhere in a string.

string.matches(regex) returns false, although I think it should be true

I am working with Java regular expressions.
Oh, I really miss Perl!! Java regular expressions are so hard.
Anyway, below is my code.
oneLine = "{\"kind\":\"list\",\"items\"";
System.out.println(oneLine.matches("kind"));
I expected "true" to be shown on the screen, but I could only see "false".
What's wrong with the code? And how can I fix it?
Thank you in advance!!
String#matches() takes a regex as parameter, in which anchors are implicit. So, your regex pattern will be matched at the beginning till the end of the string.
Since your string does not start with "kind", so it returns false.
Now, as per your current problem, I think you don't need to use regex here. Simply using String#contains() method will work fine: -
oneLine.contains("kind");
Or, if you want to use matches, then build the regex to match complete string: -
oneLine.matches(".*kind.*");
The .matches method is intended to match the entire string. So you need something like:
.*kind.*
Demo: http://ideone.com/Gb5MQZ
Matches tries to match the whole string (implicit ^ and $ anchors), you want to use contains() to check for parts of the string.

Regex to match pattern with subdomain in java gives issues

I am trying to match the sub domain of an url using http://([a-z0-9]*.)?example.com/.* which works perfectly for these cases.
http://example.com/index.html
http://test.example.com/index.html
http://test1.example.com/index.html
http://www.example.com/122/index.html
But the problem is it matches for this URL too.
http://www.test.com/?q=http://example.com/index.html
if an URL with another domain has the URL in path it matches.Can any one tell me how to match for current domain only. getting the host will work but i need to match full URL.
Are you aware that . matches any character?
If you use the regex
http://([a-z0-9]*\.)?example\.com/.*
(or, as a Java String)
"http://([a-z0-9]*\\.)?example\\.com/.*"
it should work because now the ?q= part won't be matched.
This assumes that you're using the .matches() method which forces the entire string to match. Otherwise, add a ^ at the start of the regex.
simplest way would be:
^http://([a-z0-9]*?\.)?example\.com/.*
the ^ matches the starting position within the string. Dont confuse it with [^ ] though.

Anyone know how to test an entire String for a match using Java regex?

I would like to search a String for an entire match. In other words, if String s = "I am coding", and I type in that I am searching for "am" nothing should get returned. I need the exact String in order to get a match. In other words, I would have to type in"I am coding" exactly in order for a match to be returned.
I need the regex pattern for this, since I am using RowFiler.regexFilter(...).
Have you tried this: ^I am coding$?
The regex, if it doesn't contain characters to escape is as what you are looking for: any character maches for itself and two next characters means concatenation. So, in this case, "\AI am coding\z" is your answer..
On the Regex side of things:
using the start of string anchor ^ and end of string anchor $ at the beginning and the end of your search pattern (respectively) to ensure that the search string doesn't contain anything else (i.e. it equals the pattern you're trying to match. Regex:
^I am Coding$
Ref: http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm

Categories