I have the following REGEX
/^(?!.* )(?=.*[!##$\.%^&])(?=.*\d)(?=.*[A-Za-z])$/
it should not allow white space and contains a letter a digit and a character.
But I would like to have the following
do not contain space, and contains any of [!##$\.%^&], digits and character, so aaaaaaaa or !!!!!!!! would work.
but I can7t find how to validate the lot
With your shown samples, could you please try following. Here is Online regex demo
^[!#$a-z.%^&\dA-Z#]+$
Explanation: Simply looking for !#$a-z.%^&\dA-Z# characters from starting to end of the string if only these come then match the string if anything else is coming apart from these then don't match the string.
Related
I am new to regex, basically I'd like to check if a word has ONLY one colons or not.
If has two or more colons, it will return nothing.
if has one colon, then return as it is. (colon must be in the middle of string, not end or beginning.
(1)
a:bc:de #return nothing or error.
a:bc #return a:bc
a.b_c-12/:a.b_c-12/ #return a.b_c-12/:a.b_c-12/
(2)
My thinking is, but this is seems too complicated.
^[^:]*(\:[^:]*){1}$
^[-\w.\/]*:[-\w\/.]* #this will not throw error when there are 2 colons.
Any directions would be helpful, thank you!
This will find such "words" within a larger sentence:
(?<= |^)[^ :]+:[^ :]+(?= |$)
See live demo.
If you just want to test the whole input:
^[^ :]+:[^ :]+$
To restrict to only alphanumeric, underscore, dashes, dots, and slashes:
^[\w./-]+:[\w./-]+$
I saw this as a good opportunity to brush up on my regex skills - so might not be optimal but it is shorter than your last solution.
This is the regex pattern: /^[^:]*:[^:]*$/gm and these are the strings I am testing against: 'oneco:on' (match) and 'one:co:on', 'oneco:on:', ':oneco:on' (these should all not match)
To explain what is going on, the ^ matches the beginning of the string, the $ matches the end of the string.
The [^:] bit says that any character that is not a colon will be matched.
In summary, ^[^:] means that the first character of the string can be anything except for a colon, *: means that any number of characters can come after and be followed by a single colon. Lastly, [^:]*$ means that any number (*) of characters can follow the colon as long as they are not a colon.
To elaborate, it is because we specify the pattern to look for at the beginning and end of the string, surrounding the single colon we are looking for that only the first string 'oneco:on' is a match.
I know that there are a lot of topics like this one. I've spent a lot of hours checking expressions to make my code work. I don't really understand how regex work, so I hope you can help me out.
I want to validate this inputs (I hope I am not pushing it)
Only letters (with latin characters too)
Address (including dots, commas, colon, number sign and hyphen)
Telephone (numbers and hyphen)
like:
/[a-zA-ZÑñÁáÉéÍíÓóÚú]+$/ /* Only letters */
/[a-zA-Z0-9\sñáéíóúü .,:#-]+$/ /* Address */
/^[\d-]+$/ /* Telephone */
They work fine, when I include an special character at the end of the string but if I enter that special character between accepted characters it does not work. Allow me to write an example please:
For the "Only letters" expression:
ab[(% - Does not pass
a[(%b - It pass and it shouldn't!
Thanks a lot for your time, any help will be appreciate!
You forgot the ^ start of string anchor at the beginning of the 2 first patterns.
See demo 1:
^[a-zA-ZÑñÁáÉéÍíÓóÚú]+$
^
Same with the second regex. There, you also have a literal space and \s, so literal space can be removed:
^[a-zA-Z0-9\sñáéíóúü.,:#-]+$
^
See demo 2
And as for your third regex, it is not optimal since it will match ----1123.
Use
/^(?:\d+-)+\d+$/
See demo 3. Here, we match sequences of digits and hyphen (with (?:\d+-)+) and then a sequence of digits, from beginning till end.
The expression /[..]+$/ says that the test subject must have any of the characters (..) at its end. $ symbolises the end of the string. The beginning of the string does not have to match. If you want to enforce that for the entire string, use the beginning anchor as well:
/^[..]+$/
This now says the string must have any of the characters (..) between its beginning and end, and there's no room for anything else.
You're already doing this for the telephone regex.
I am trying to validate a sentence. It starts with alphabets, contains numbers and special characters like '-,() and may end with : or . I am trying to find an expression that can match the following pattern.
I'm trying to-achieve such(this), kind of pattern:
I have tried using ^[a-zA-Z]+([ '/-]{0,1}+([()]{0,1}[,]{0,1})+[a-zA-Z0-9.]+[:]??)+$ , but am facing a problem at getting ',' after a closing ')' followed by space.
Can someone please help me.
Thanks
Let's make sure I understand what you're going for:
Your regex will match an entire sentence, meaning any string that starts with a letter of the alphabet and ends with a colon or period.
This sentence may contain numbers and special characters; really any character except for a colon or period, which would signal the end of the sentence.
If so, then all you need is this:
^[A-Za-z][^\.:]*[\.:]$
^ matches the beginning of the string.
[A-Za-z] matches any letter of the alphabet, upper- or lower-case.
[^\.:]* matches 0 or more characters of any kind as long as they are not a colon or a period.
[\.:] matches a colon or a period.
$ matches the end of the string.
This will only work if the string you're matching is the sentence and nothing else. To match a sentence that is part of a larger string, try removing the ^ at the beginning and the $ at the end, and using the /g (multiple matches) tag if it meets your needs.
I'm trying to match the last four characters (alphanumeric) of all words beginning with the sequence &c.
For instance, in the string below, I'd like to match the pieces in bold:
Colour one is &cFF2AC3 and colour two is &c22DE4A.
Can anybody help me with the correct regex expression? I've spent hours on this great resource to no avail.
it looks like hexadecimal numbers, so use this pattern
&c[0-9A-F]{2}\K([0-9A-F]{4})
DEMO
This:
/(?i)\s*&c(?:[a-z0-9]{2})([a-z0-9]{4})\b/
append a g to the end of it if you want it to find all matches in a given text
Try this
/(?:^| )&c\w*(\w{4})\b/
If you want to try it in the regex tester you linked to, make sure to use the g modifier to see all matches.
Explanation: (?:^| ) matches either a space or the start of the string, &c\w* matches the ampersand and the the first however many characters of the word, and then \w{4} captures the last 4 characters. \b on the end asserts a word break (a "non-word" character or the end of the string).
I am doing a file search and replace for occurrences of specific words in perl. I'm not usually much of a perl or regex user. I have searched for other regex questions here but I couldn't find one which was quite right so I'm asking for help. My search and replace currently looks like this:
s/originalword/originalword_suffix/g
This matches cases of originalword that appear in the middle of another word, which I don't want. In my application of search and replace, a whole word can be defined as having the letters of the latin alphabet in lowercase or capital letters and the digits 0-9 and the symbol _ in any uninterrupted sequence. Anything else besides these characters, including any other symbols or any form of whitespace including line breaks or tabs, indicate operations or separators of some kind so they are outside the word boundaries. How do I modify my search and replace to only match whole words as I've defined them, without matching substrings?
Examples:
in the case that originalword = cat and originalword_suffix = cat_tastic
:cat { --> :cat_tastic {
:catalog { --> no change
Use the \b anchor to match only on a word boundary:
s/\bcat\b/cat_tastic/g
Although Perl has a slightly different definition of what a "word" is. Reading the perlre reference guide a couple of times might help you understand regexps a bit better.
Running perl -pi -e "YOUR_REGEXP" in a terminal and entering in lines of text can help you understand and debug what a particular regexp is doing.
You could try:
s/([^0-9a-z_])([0-9a-z_]+)([^0-9a-z_])/$1$2_tastic$3/gi
Basically, a non-word character, then a set of word characters, followed by a non-word character. The $1,$2,$3 represent the captured groups, and you replace $2 with $2_suffix.
Hope that helps, not a perl guy buy pretty regex-savvy. Note that the above will fail if the word is the very first or very last thing in a string. Not sure if perl regexen allow the syntax, but if so, fixing the first/last issue could be done with:
s/(^|[^0-9a-z_])([0-9a-z_]+)([^0-9a-z_]|$)/$1$2_tastic$3/gi
Using ^ and $ to match beginning/end of string.
See the example on this page which explains boundary matchers
Enter your regex: \bdog\b
Enter input string to search: The dog plays in the yard.
I found the text "dog" starting at index 4 and ending at index 7.
Enter your regex: \bdog\b
Enter input string to search: The doggie plays in the yard.
No match found.