I need a RegEx pattern which will be sent by the client where the starting characters will be alphanumeric, the length of this starting String will be defined by the number after this String. This is followed by a special character which will always be a single character. This is again followed by a variable length string of alphanumeric characters.
I have come closest to the below String and formats.
[A-Za-z0-9]{4}-[A-Za-z0-9]{5} - RegEx Input String
[A-Za-z0-9]{2}#[A-Za-z0-9]{6} - RegEx Input String
[0-9]{3}#[0-9]{5} - RegEx Input String
[a-z]{5}#[a-z]{5} - RegEx Input String
[A-Z]{4}#[a-z]{4} - RegEx Input String
[\w]{\d{1,1}}(\S{1,1})[\w]{\d{1,1}} - RegEx Format
Is the above pattern and format correct?
Can we validate the RegEx input string against the required RegEx format?
This is a web service which will have an input as [A-Za-z0-9]{4}-[A-Za-z0-9]{5}. I need two things here. First, how do I validate this input to see if it matches the format I want and the proceed. The format is the one I mentioned above as RegEx format.
This regular expression should match the subset of regular expressions you're interested in :
\[(?:[a-zA-Z0-9](?:-[a-zA-Z0-9])?)*\](?:\{\d\})?\S\[(?:[a-zA-Z0-9](?:-[a-zA-Z0-9])?)*\](?:\{\d\})?
Let's break it down :
it matches a line which contains in sequence a character class, an optional quantifier, a separator, a second character class and its second optional quantifier
the separator is any non-whitespace character, \S (you might want to change that to something more specific, or which includes some whitespaces)
the optional quantifier is easy, it's a digit surrounded with literal curly brackets, the whole enclosed in an unbound group we use to make it optional : (?:\{\d\})?. Note that this will not accept multiple digits length, so you might want to change the \d to \d+, nor the more specific {m,n} range quantifier.
a character class is a sequence of 0 or more characters or character-ranges, enclosed in literal brackets.
a character is a letter or digit : [a-zA-Z0-9](?:-[a-zA-Z0-9])? when the unbounded group isn't matched
a character range is a character followed by the literal - followed by another character : [a-zA-Z0-9](?:-[a-zA-Z0-9])? when the unbounded group is matched
Related
I have a big Java regex pattern composed of multiple subpatterns concatenated by OR (|). I want to allow multiple delimiters anywhere in between the numbers.
For example, I have the following pattern "(3[47][0-9]{13})|(56022[1-5][0-9]{10}|(5610)[0-9]{12})". How do I allow the following delimiters: equal to (=), backslash (\), dot (.), hyphen (-) and white space ().
These delimiters can appear anywhere (except start and end) and any number of times in between the numbers which match the numeric pattern.
You will have to insert [\s=\\.-]* pattern (it matches zero or more whitespaces, =, \, . and -) in between all digit matching patterns and convert \d{X} into \d(?:[\s=\\.-]*\d){X-1} patterns:
(3[\s=\\.-]*[47][\s=\\.-]*[0-9](?:[\s=\\.-]*[0-9]){12})|(5[\s=\\.-]*6[\s=\\.-]*0[\s=\\.-]*2[\s=\\.-]*2[\s=\\.-]*[1-5][\s=\\.-]*[0-9](?:[\s=\\.-]*[0-9]){9}|(5[\s=\\.-]*6[\s=\\.-]*1[\s=\\.-]*0)[\s=\\.-]*[0-9](?:[\s=\\.-]*[0-9]){11})
See the regex demo
Do not forget to double the backslashes when using the pattern inside a Java string literal:
String part_of_regex = "(3[\\s=\\\\.-]*[47][\\s=\\\\.-]*[0-9](?:[\\s=\\\\.-]*[0-9]){12})|(5[\\s=\\\\.-]*6[\\s=\\\\.-]*0[\\s=\\\\.-]*2[\\s=\\\\.-]*2[\\s=\\\\.-]*[1-5][\\s=\\\\.-]*[0-9](?:[\\s=\\\\.-]*[0-9]){9}|(5[\\s=\\\\.-]*6[\\s=\\\\.-]*1[\\s=\\\\.-]*0)[\\s=\\\\.-]*[0-9](?:[\\s=\\\\.-]*[0-9]){11})";
I have a String ex=1,2,3,4,5-7,8,9,10-15,34,898
In the above string my regex has to validate the following things
ex String should contain only numeric values(0-9) and two special characters
,- alone.
Regex should validate ex should not start with space,cama(,) and
hyfun(-),same should not end.
After cama(,) should have numeric value alone.
String ex should support only positive numbers 0-9.
After hyfun(-) there should be a positive numeric value alone.
It should not allow space any where in the string.
hyfun(-) symbol is the range indicator in the above ex String, so it should
prefix and suffix with a positive numeric value.
cama(,) is a separator for each element in the string, so it should succeeded with positive numeric value.
so for i have tried with individual regex which making my code clumsy and few cases its failing
//Regex Pattern for validating number alone as its starting and ending of the string
Pattern digits = Pattern.compile ("^[0-9](.*[0-9])?$");
//Regex Pattern for validating special character along with the digits alone
Pattern special = Pattern.compile("^[0-9,-]*$");
//Regex Pattern for validating only positive numeric values alone
Pattern positiveNumeric = Pattern.compile("^\\d+$");
Here is a general regex pattern which should work:
^\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*$
Demo
The quantity \d+(?:-\d+)? says to match one or more digits, followed optionally by a hyphen and then one or more other digits. Then, we append this to end of the pattern:
(?:,\d+(?:-\d+)?)*
This matches a comma followed by another digit/range of digits group, zero or more times.
Note that the ^ and $ anchors may not be necessary in your Java code if you are using, e.g. String#matches, which automatically adds these anchors.
I have a Java regex:
^[a-zA-Z_][a-zA-Z0-9_]{1,126}$
It means:
Begin with an alphabetic character or underscore character.
Subsequent characters may include letters, digits or underscores.
Be between 1 and 127 characters in length.
Now, I want to replace a string having characters not in that regex with a underscore.
Example:
final String label = "23_fgh99##";
System.out.println(label.replaceAll("^[^a-zA-Z_][^a-zA-Z0-9_]{1,126}$", "_"));
But the result is still 23_fgh99##.
How can I "convert" it to _3_fgh99__?
Use this code:
final String label = "23_fgh99##";
System.out.println(label.replaceAll("^[^a-zA-Z_]|(?<!^)[^a-zA-Z0-9_]", "_"));
It outputs _3_fgh99__.
To remove what is "not in the original pattern", you need to negate the first character class and only check a character at the beginning (^[^a-zA-Z_]), and then check other characters not at the beginning with the negated second character class ((?<!^)[^a-zA-Z0-9_]). Then, we just use an alternation symbol | to apply both patterns in 1 replacement operation.
I am trying to match everything but garbage values in the entire string.The pattern I am trying to use is:
^.*(?!\w|\s|-|\.|[#:,]).*$
I have been testing the pattern on regexPlanet and this seems to be matching the entire string.The input string I was using was:
Vamsi///#k03#g!!!l.com 123**5
How can I get it to only match everything but the pattern,I would like to replace any string that matches with an empty space or a special charecter of my choice.
The pattern, as written, is supposed to match the whole string.
^ - start of string.
.* - zero or more of any character.
(?!\w|\s|-|\.|[#:,]) - negative look-ahead for some characters.
.* - zero or more of any character.
$ - end of string.
If you only want to match characters which aren't one of the supplied characters, try simply:
[^-\w\s.#:,]
[^...] is a negated character class, it will match any characters not supplied in the brackets. See this for more information.
Test.
am using regex expression to check if a string contains white space.
my regex is : ^\\s+$
for example if my string is my name then regex matches should return true.
but it is returning true only if my string contains only spaces no other character.
How to check if a string contains a whitespace or tab or carriage return characters in between/start/end of some string.
^(.*\s+.*)+$ seems to work for me. Accepts anything as long as there is at least one space in the string. This will match the entire string.
If you only want to check for the presence of a space, you can just use \s without any begin or end markers in the string. The difference is that this will only match the individual spaces.
Your regex is not correct.
That's a string representing a regular expression. (as tchrist pointed out correctly)
The corresponding pattern that you get when using Pattern.compile() matches only strings containing one or more whitespace characters, starting from the beginning until the end. Thus, the matching string only consists of whitespace characters.
Try this string instead for Pattern.compile():
"\\s+"
The difference is that without the anchors "^" and "$" there may be other characters around the whitespace character. The whitespace character(s) may be everywhere in the string.
Using this pattern-string the whitespace character(s) must be at the beginning:
"^\\s+"
And here the sequence of whitespace characters has to be at the end:
"\\s+$"
Use org.apache.commons.lang.StringUtils.containsAny(). See http://commons.apache.org/lang/api-3.1/org/apache/commons/lang3/StringUtils.html.