I want to do validation for a String which can only contains alphanumeric and only one special character. I tried with (\\W).{1,1}(\\w+).
But it is true only when I start with a special character. But I can have one special character at any place in String.
Use the ^ and $ anchors to instruct the regex engine to start matching from the beginning of the string and stop matching at the end of the string, so taking your regex:
^(\\W).{1,1}(\\w+)$
Please take a look at this Oracle (Java) tutorial on regular expressions.
Try this regexp: \w*\W?\w* (Java string: "\\w*\\W?\\w*")
This expression has a drawback of matching zero-length strings. If your input must have exactly one special character, remove the question mark ? from the expression.
use matcher.find() and not matcher.match() and search for \\w and remove plus (+) because it will match all alphanumeric characters sequence in your string.If your string contains only them, your regex will match whole string.
if I understand your regex correctly, this could solve your problem:
([\w]+)([^\w])([\w]+)
Related
I would like to match URL strings which can be specified in the following manner.
xxx.yyy.com (For example, the regular expression should match all strings like 4xxx.yyy.com, xxx4.yyy.com, xxx.yyy.com, 4xxx4.yyy.com, 444xxx666.yyy.com, abcxxxdef.yyy.com etc).
I have tried to use
([a-zA-Z0-9]+$)xxx([a-zA-Z0-9]+$).yyy.com
([a-zA-Z0-9]*)xxx([a-zA-Z0-9]*).yyy.com
But they don't work. Please help me write a correct regular expression. Thanks in advance.
Note: I'm trying to do this in Java.
If you want to make sure there is xxx and you want to allow all non whitespace chars before and after. If you want to match the whole string, you could add anchors at the start and end.
Note to escape the dot to match it literally.
^\S*xxx\S*\.yyy\.com$
^ Start of string
\S*xxx\S* Match xxx between optional non whitespace chars
\.yyy Match .yyy
\.com Match .com
$ End of string
Regex demo
In Java double escape the backslash
String regex = "^\\S*xxx\\S*\\.yyy\\.com$";
Or specify the characters on the left and right that you would allow to match in the character class:
^[0-9A-Za-z!##$%^&*()_+]*xxx[0-9A-Za-z!##$%^&*()_+]*\.yyy\.com$
Regex demo
I don't have an experience on Regular Expressions. I need to a regular expression which doesn't allow to repeat of special characters (+-*/& etc.)
The string can contain digits, alphanumerics, and special characters.
This should be valid : abc,df
This should be invalid : abc-,df
i will be really appreciated if you can help me ! Thanks for advance.
Two solutions presented so far match a string that is not allowed.
But the tilte is How to prevent..., so I assume that the regex
should match the allowed string. It means that the regex should:
match the whole string if it does not contain 2
consecutive special characters,
not match otherwise.
You can achieve this putting together the following parts:
^ - start of string anchor,
(?!.*[...]{2}) - a negative lookahead for 2 consecutive special
characters (marked here as ...), in any place,
a regex matching the whole (non-empty) string,
$ - end of string anchor.
So the whole regex should be:
^(?!.*[!##$%^&*()\-_+={}[\]|\\;:'",<.>\/?]{2}).+$
Note that within a char class (between [ and ]) a backslash
escaping the following char should be placed before - (if in
the middle of the sequence), closing square bracket,
a backslash itself and / (regex terminator).
Or if you want to apply the regex to individual words (not the whole
string), then the regex should be:
\b(?!\S*[!##$%^&*()\-_+={}[\]|\\;:'",<.>\/?]{2})\S+
[\,\+\-\*\/\&]{2,} Add more characters in the square bracket if you want.
Demo https://regex101.com/r/CBrldL/2
Use the following regex to match the invalid string.
[^A-Za-z0-9]{2,}
[^\w!\s]{2,} This would be a shortest version to match any two consecutive special characters (ignoring space)
If you want to consider space, please use [^\w]{2,}
(Edit, I said 'digit', I should have said 'alphanumeric char')
How do I extract a postfix from a string from a list of possibles (,X,,Y,,X),),Y). All need to be preceded by a alphanumeric character to be valid but the character is not to be extracted:-
What I am using is \w(,X|,Y|,X\)|\),Y){1}$ but this includes the preceding character (\w) in the extracted value.
(Unit tests pass but it's not sophisticated enough to test the returned match)
https://regex101.com/r/4Ggu7z/5/tests
Converting my comment to an answer.
You can use a negative lookahead instead of matching character in your regex. Here is working regex:
(?<=\w)(,[XY]|,X\)|\),Y)$
RegEx Demo
How to split a string using single occurrence of a delimiter disregarding multiple occurrences?
For example, if the string contains
aaa, bbb,,ccc, ddd
I would like to split the string as follows:
aaa
bbb,,ccc
ddd
Tried using Regex with split() but unable to acquire the desired result.
Came across the solution in Javascript here: Split string with a single occurence (not twice) of a delimiter in Javascript. Is it possible to achieve the same in Java, with or without Regex?
String.split() accepts regular expressions as delimiters so you could use the following pattern :
(?<!,),(?!,)
This regex matches a comma that is neither preceded nor followed by a comma.
You can see it in action here : https://ideone.com/CmtAzX
If you want to trim the leading spaces at the same time you can use (?<!,),(?!,) * as mentioned by Nicolas Filotto.
Regex allows you to specify that a given symbol is neither preceded nor followed by another specified symbol. In your case you should use (?<!,),(?!,). In general (?<!x)y(?!z) would find 'y' if it is neither preceeded by 'x' nor followed by 'z'.
Basically my question is this, why is:
String word = "unauthenticated";
word.matches("[a-z]");
returning false? (Developed in java1.6)
Basically I want to see if a string passed to me has alpha chars in it.
The String.matches() function matches your regular expression against the whole string (as if your regex had ^ at the start and $ at the end). If you want to search for a regular expression somewhere within a string, use Matcher.find().
The correct method depends on what you want to do:
Check to see whether your input string consists entirely of alphabetic characters (String.matches() with [a-z]+)
Check to see whether your input string contains any alphabetic character (and perhaps some others) (Matcher.find() with [a-z])
Your code is checking to see if the word matches one character. What you want to check is if the word matches any number of alphabetic characters like the following:
word.matches("[a-z]+");
with [a-z] you math for ONE character.
What you’re probably looking for is [a-z]*