I want to create to regular expression that allows all combinations of characters and digits and also dots but not a dot at the beginning. So valid strings could be "1.2.3" or "1.b.34d" or something. But I want to disallow strings like "." or ".1.2.3".
I'm trying with
[^.]{0,1}[ a-zA-Z_\\-\\.0-9]*
but this doesn't work. Anyone any better idea?
You may use
s.matches("(?!\\.)[\\w\\s.-]*")
The pattern will match a string that does not start with a . due to the negative lookahead (?!\\.) and contains zero or more word chars (letters, digits or underscores - \w), whitespaces (\s), . or - symbols. Note you do not need ^/\A, nor $/\z since the String#matches() method anchors the pattern by default.
NOTE: To make this pattern Unicode-aware, add (?U) (a Pattern.UNICODE_CHARACTER_CLASS embedded flag option) at the start of the pattern: s.matches("(?U)(?!\\.)[\\w\\s.-]*").
Use negative lookahead:
^(?!\\.)[\w .-]+$
\w is equivalent to [a-zA-Z0-9_]
Please try ^[^\.][ a-zA-Z_\\-\\.0-9]*
In regular expressions the dot character means "any character". To match for the litteral you need to escape it.
^[^\\.][ a-zA-Z_\\-\\.0-9]*
Related
I have the follow pattern to validate a string, it has to validate 4 letters, 6 numbers, 6 letters and 2 alphanumerics, but with my current pattern I cant get a valid test
Pattern.compile("[A-Za-z]{4}\\d{6}\\w{6}\\[A-ZÑa-zñ0-9\\- ]{2}");
I think my pattern it's wrong, because I'm not shure about this [A-ZÑa-zñ0-9\\- ]{2}
Can you please help me?
You can use pattern:
^[a-zA-Z]{4}[0-9]{6}[a-zA-Z]{6}[a-zA-Z0-9]{2}$
Check it live here.
In your expression you are using \w+, which does not only match digits and alphabetic characters, but also underscores _.
A few things off on your regex.
You have extra backslashes in your digit and word matching. Change from \\d to \d and \\w to \w.
The \\ is not needed.
Your end regex is invalid syntax. Just remove the "\\- " bit.
You can also slim down your initial part to be \w instead of [A-Za-z]. So, you're new regex should look like:
"\w{4}\d{6}\w{6}[A-ZÑa-zñ0-9]{2}"
That is if you're okay with the only non-ascii characters being Ñ and ñ in your last two alphanumerics.
Could anyone please provide me with a regular expression to allow only digits (0-9) excluding even '.' (decimal point) in a textbox in JSP using javascript. I would be using it to replace the resricted characters with '' (empty string).
I tried few but they are not restricitng the DOT.
Thanks in advance
regex pattern would be:
/^[0-9]+$/
You need to anchor the regex:
/^\d*$/
to make sure that the entire string consists of digits.
Without ^ and $, the regex would match 1 (and 234) in 1.234.
I would use the class \d, it includes only digits. If you want to replace all non-digits you would need to replace \D+ with "".
I am using the following regex:
[a-zA-Z0-9-#.()/%&\\s]{0,19}.
The requirement for the field is it should allow any thing and the field size should be 19.
Let me know if any corrections.Any help is appreciated.
You simply need to escape the special characters. Try:
[a-zA-Z0-9\-#\.\(\)\/%&\s]{0,19}
You can test your regular expressions on http://rubular.com/
Your regex is incorrect in at least one way - if you're considering a hyphen to be a "special character", then you should put it at the beginning or end of the range. So: [a-zA-Z0-9#.()/%&\s-]{0,19}.
Characters that are "special" within the context of the regex itself are often not parsed if they're inside a range. So you're fine with ., ( and ). But check your parser to make sure that it understands what \s means. It might be simpler just to put a space.
Also, if your regex parser tends to delimit the regex with slashes, then you may have to escape the slash in the middle of the range: [a-zA-Z0-9#.()\/%&\s-]{0,19}.
Just escape the dash - or put it at the begining or at the end of the character class:
[a-zA-Z0-9\\-#.()/%&\\s]{0,19}
or
[-a-zA-Z0-9#.()/%&\\s]{0,19}
or
[a-zA-Z0-9#.()/%&\\s-]{0,19}
I need a pattern to match words like APPLE: or PEAR:
[A-Z][:] will match the R: but not the whole word and thus gives me a false when I try to match.
Can anybody help?
You want to match one or more capital letter which means you need to use a +. Also your : doesn't need to be in a character class:
[A-Z]+:
Just add a "quantifier":
/[A-Z]+:/
Note you don't need a character class for a single character.
How about \b[A-Z]+:? The \b is for checking a word boundary btw.
\b can be used to capture characters only in a word-boundary ie between the start and end of a word.
[A-Z] indicates a range of characters and specifying A-Z specifically matches the range of characters from capital A to capital Z. In other words, only upper case letters.
End the query by trying to match a semicolon and you'll find matches of a capital letter word immediately followed by a single semi-colon.
You can use the regular expression in Java like below.
import java.util.regex.*;
public class RegexExample {
System.out.println(
Pattern.matches("\b[A-Z]+:", "data: stuff, MIX!: of, APPLE: or, PEAR: or, PineAPPLes: yay!")
);
}
I recommend finding an online playground for regular expressions. Iterating and experimenting with regexes for a project can be a fast way to learn the limitations and find ways to simplify or improve an expression.
you need to use the + operator to get a match to all characters in the group
try with regex:
[A-Z]+\:
How can I create a regular expression to search strings with a given pattern? For example I want to search all strings that match pattern '*index.tx?'. Now this should find strings with values index.txt,mainindex.txt and somethingindex.txp.
Pattern pattern = Pattern.compile("*.html");
Matcher m = pattern.matcher("input.html");
This code is obviously not working.
You need to learn regular expression syntax. It is not the same as using wildcards. Try this:
Pattern pattern = Pattern.compile("^.*index\\.tx.$");
There is a lot of information about regular expressions here. You may find the program RegexBuddy useful while you are learning regular expressions.
The code you posted does not work because:
dot . is a special regex character. It means one instance of any character.
* means any number of occurrences of the preceding character.
therefore, .* means any number of occurrences of any character.
so you would need something like
Pattern pattern = Pattern.compile(".*\\.html.*");
the reason for the \\ is because we want to insert dot, although it is a special regex sign.
this means: match a string in which at first there are any number of wild characters, followed by a dot, followed by html, followed by anything.
* matches zero or more occurrences of the preceding token, so if you want to match zero or more of any character, use .* instead (. matches any char).
Modified regex should look something like this:
Pattern pattern = Pattern.compile("^.*\\.html$");
^ matches the start of the string
.* matches zero or more of any char
\\. matches the dot char (if not escaped it would match any char)
$ matches the end of the string