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.
Related
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
I'm building a function to validate usernames, and in this case I want to accept alphabetic characters only. I'm matching the provided user input against this regex:
[1-9!##$%&*()_+=|<>?{}\\[\\]~-,]
This is the method that makes use of the regex:
public static String purgeInvalidLogin(String failedLogin, String pattern) {
Pattern special = Pattern.compile (pattern);
String purgedLogin = failedLogin.replaceAll(special.pattern(), ""); // remove any special characters before moving on
purgedLogin = StringUtils.deleteWhitespace(purgedLogin);
return purgedLogin;
}
However when trying to run this I get this message:
Illegal character range near index 25 [!##$%&*()_+=|<>?{}[]~-,] ^
which only happened once I added the comma. I've also tried the expression [!##$%&*()_+=|<>?{}[]~-\,] (escaping the comma) to no avail. I'm wondering how I can use the regex properly to exclude commas making use of my method above.
Thanks in advance.
Escape the hyphen just before it. It is interpreted as defining a range of characters, as soon as you add another character (the comma) after it.
[1-9!##$%&*()_+=|<>?{}\\[\\]~\\-,]
You want to accept only alpha chars and you are doing this by listing every possible illegal character. I think you have got this backwards and it would better to look for what you do want (which would be a much shorter regex) and flag non matches.
I am trying to formulate a regex for the following scenario :
The String to match : mName87.com
So, the string may consist of any number of alpha numeric characters , but can contain only a single dot anywhere in the string .
I formulated this regex : [a-zA-Z0-9.], but it matches even multiple dots(.)
What am i doing wrong here ?
The regex you provided matches only a single character in the whole string you're trying to validate. There are a few things to take care of in your scenario
You want to match over the whole string, so your regex must start with ^ (beginning of the string) and end with $ (end of the string).
Then you want to accept any number of alpha-numeric characters, this is done with [a-zA-Z0-9]+, here the + means one or more characters.
Then match the point: \. (you must escape it here)
Finally accept more characters again.
All together the regex would then be:
^[a-zA-Z0-9]+\.[a-zA-Z0-9]+$
You can use this regex:
\\w*\\.\\w*
You can try here
Try with:
^([a-zA-Z0-9]+\.)+[a-zA-Z]$
use this regular expression ^[a-zA-Z0-9]*\.[a-zA-Z0-9.]*$
EDITED:
Try
([a-zA-Z0-9]+\.[a-zA-Z0-9]+)|(\.[a-zA-Z0-9]+)|([a-zA-Z0-9]+\.)
That is: [a word that ends with a dot] OR [two words and the dot in the middle] OR [a word that starts with a dot]
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
Why this code would fail?
assertTrue(Pattern.matches("[^a-zA-Z0-9]", "abc;"));
Because the .matches() method tries to match the entire string, and your regex doesn't match the entire string, only the semicolon. The Matcher.find() method would work (in this case: find a character that is not a letter between a and z nor a number between 0 and 9. Of course, it will also find á, ö etc.)
What is it you really want to do?
If fails because matches tries to match the complete string, your regexp matches 1 character which is not in the character ranges you list, if you change to:
assertTrue(Pattern.compile("[^a-zA-Z0-9]").matcher("abc;").find());
it should assert true.
Because Pattern.matches() is equivalent to the corresponding pattern being compiled and fed to Matcher.matches() which, as specified, checks to see if the entire input matches the pattern. If you only want to match part of the input, you'll want to use Matcher.find() instead.
try "^[a-zA-Z0-9]" as pattern
Because when you put the ^ inside, it means any char not in a-z or A-Z. What you want is ^[a-zA-Z0-9]