I need a regular expression which matches lines with only 4(four) hyphens and 13 digits(0-9). The order is undefined.
I have regex like:
^([0-9\u2013-]{17})$
But, when I receive strings as
----123456789---- or 1-2-3-4-5-6-7-8-9
matching is true but it must be false for me.
Could you please explain what I need use in order to matches were only with strings like 123-345-565-45-67 or 123-1-34-5435-45- or ----1234567890123 etc?
Try this regex:
^(?=(?:[^-]*-){4}[^-]*$)(?=(?:\D*\d){13}\D*$).*$
Click for Demo
Explanation:
^ - asserts the start of the line
(?=(?:[^-]*-){4}[^-]*$) - positive lookahead to make sure that there are only 4 occurrences of - present in the string
(?=(?:\D*\d){13}\D*$) - positive lookahead to make sure that there are 13 occurrences of a digit present in the string
.* - once the above 2 lookaheads are satisified, match 0+ occurrences of any character except a newline character
$ - asserts the end of the line
Escape \ with another \ in JAVA
Related
I need to match a string with the following constraints:
At least one alphanumeric character
Forbid specific characters (^*#:;)
Forbid dot at the end
I have the next pattern:
^[^*#:;]*[\p{Alnum}]+[^*#:;]*[^.*#:;]$
The problem is that when I have an alphanumeric character at the end, the string will not match the pattern.
For example:
$$$....1$ will match the pattern.
$$$....$1 will not.
As far as I understand, the problem is that [\p{Alnum}]+ does not check the last character.
Is there any possible way to do this with one regexp?
It seems the following should tick your boxes:
^(?=.*\p{Alnum})(?!.*[*#:;]).+(?<!\.)$
Where:
^ - Start string anchor.
(?=.*\p{Alnum}) - Postive lookahead to match at least a single alphanumeric character.
(?!.*[*#:;]) - Negative lookahead to prevent any of the characters mentioned in the character class.
.+ - 1+ characters other than newline.
(?<!\.) - Negative lookbehind to prevent a dot before;
$ - End string anchor.
See the online demo
Alternatively use a negated character class as you were doing instead of the negative lookahead:
^(?=.*\p{Alnum})[^*#:;\n]+(?<!\.)$
^ - Start string anchor.
(?=.*\p{Alnum}) - Postive lookahead to match at least a single alphanumeric character.
[^*#:;\n]+ - 1+ characters other than those mentioned in the character class.
(?<!\.) - Negative lookbehind to prevent a dot before;
$ - End string anchor.
See the online demo
I have some URL link and tried to replace all non-integer values with integers in the end of the link using regex
The URL is something like
https://some.storage.com/test123456.bucket.com/folder/80.png
Regex i tried to use:
Integer.parseInt(string.replaceAll(".*[^\\d](\\d+)", "$1"))
Output for that regex is "80.png", and i need only "80". Also i tried this tool - https://regex101.com. And as i see the main problem is that ".png" not matching with my regex and then, after substitution, this part adding to matching group.
I'm totally noob in regex, so i kindly ask you for help.
You may use
String result = string.replaceAll("(?:.*\\D)?(\\d+).*", "$1");
See the regex demo.
NOTE: If there is no match, the result will be equal to the string value. If you do not want this behavior, instead of "(?:.*\\D)?(\\d+).*", use "(?:.*\\D)?(\\d+).*|.+".
Details
(?:.*\D)? - an optional (it must be optional because the Group 1 pattern might also be matched at the start of the string) sequence of
.* - any 0+ chars other than line break chars, as many as possible
\D - a non-digit
(\d+) - Group 1: any one or more digits
.* - any 0+ chars other than line break chars, as many as possible
The replacement is $1, the backreference to Group 1 value, actually, the last 1+ digit chunk in the string that has no line breaks.
Line breaks can be supported if you prepend the pattern with the (?s) inline DOTALL modifier, i.e. "(?s)(?:.*\\D)?(\\d+).*|.+".
I want to check if a string consists of letters and digits only, and allow a - separator:
^[\w\d-]*$
Valid: TEST-TEST123
Now I want to check that the separator occurs only once at a time. Thus the following examples should be invalid:
Invalid: TEST--TEST, TEST------TEST, TEST-TEST--TEST.
Question: how can I restrict the repeated occurrence of the a character?
You may use
^(?:[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)?$
Or, in Java, you may use an alphanumeric \p{Alnum} character class to denote letters and digits:
^(?:\p{Alnum}+(?:-\p{Alnum}+)*)?$
See the regex demo
Details
^ - start of the string
(?: - start of an optional non-capturing group (it will ensure the pattern matches an empty string, if you do not need it, remove this group!)
\p{Alnum}+ - 1 or more letters or digits
(?:-\p{Alnum}+)* - zero or more repetitions of
- - a hyphen
\p{Alnum}+ - 1 or more letters or digits
)? - end of the optional non-capturing group
$ - end of string.
In code, you do not need the ^ and $ anchors if you use the pattern in the matches method since it anchors the match by default:
Boolean valid = s.matches("(?:\\p{Alnum}+(?:-\\p{Alnum}+)*)?");
I need a regex to match a text with special chars -,.+\/& in it. The special chars must not be more than 2 subsequent and a special char can not be followed by space. More specifically I have to cover these cases:
some text/
/some text
some /text
I came up with this regex:
^[-\/,\.+\&]{0,1}[\p{L}]+[-\/,\.+\&]{0,1}([\s\-']?[-\/,\.+\&]{0,1}[\p{L}]+)([-\/,\.+\&]{0,1})$
It matches most of the cases that I need but fails to match for instance:
some te&xt. Every help will be appreciated. Thanks.
You can use
"^(?!.*(?:[-,.+/&]\\s|[-,.+/&]{2}))[^\\s\\d]+(?:\\s+[^\\s\\d]+)*$"
See the regex demo
Explanation:
^ - start of string
(?!.*(?:[-,.+/&]\\s|[-,.+/&]{2})) - a negative lookahead that fails the match if there is a special char [-,.+/&] followed with a whitespace \s, or 2 consecutive special chars from [-,.+/&] set
[^\\s\\d]+ - 1 or more characters other than digit and whitespace
(?:\\s+[^\\s\\d]+)* - 0+ sequences of:
\\s+ - 1+ whitespaces
[^\\s\\d]+ - 1 or more characters other than digit and whitespace
$ - end of string
I found the solution:
^[-\/,\.+\&\s]{0,1}([\p{L}][-\/,\.+\&\s]{0,1})+([-\/,\.+\&\s]{0,1}([\p{L}][-\/,\.+\&\s]{0,1})+)([\p{L}][-\/,\.+\&\s]{0,1})([-\/,\.+\&\s]{0,1})$
All:
What I want to do is using Regex to match a string which only allow [A-Za-z0-9_-] and the format should be:
Started with only [A-Za-z0-9], and followed by [A-Za-z0-9_-]. There could be [_-] in the middle, but if there is any, it is only allowed once(both _ and - can exist, but each one only has one chance), and ended with [A-Za-z0-9].
I only know how to match Alphanumeric characters, a dash and an underscore, but have no idea how to limit their occurrence time.
Thanks
You can use negative lookahead:
^(?!.*(-[^-]*-|_[^_]*_))[A-Za-z0-9][\w-]*[A-Za-z0-9]$
RegEx Demo
Explanation:
^ - Line start
(?!.*(-[^-]*-|_[^_]*_)) - Negative lookahead which means fail the match if there are 2 underscore or 2 hyphens ahead
[A-Za-z0-9] - Match 1 alphanumeric character
[\w-]* - Match 0 or more of [A-Za-z0-9_-] characters
$ - Match line end