Regular expression finding letters and specific optional characters - regex

I need a regular expression that matches strings with at least one letter A-Z and, optionally, any number and combination of .-¤ (dot, dash and "sun"(what's it called in English?)).
Matched strings would be
A
AB
A-.
¤A
but NOT
-.
¤
since they don't have any letters.
My first try was of course ^[A-Z¤-.]*$ but that matches strings without letters as well.
[A-Z]+ matches strings with at least one letter
[¤.-]* matches strings that might have ¤.- in them
I've tried to combine these two last in a number of ways but haven't managed to solve my problem.
Is there a way to combine these two last regexp when I can't expect any particular order between the letters and the characters ¤.- and at the same time exclude any other characters?
Maybe groups or non-capturing groups has something to do with it, but I don't yet fully understand those.
PS I'm implementing this with the DB2 function REGEXP_LIKE.

You may use
^[A-Z.¤-]*[A-Z][A-Z.¤-]*$
Details
^ - start of string
[A-Z.¤-]* - 0+ uppercase letters, ., ¤ or -
[A-Z] - an uppercase letter
[A-Z.¤-]* - 0+ uppercase letters, ., ¤ or -
$ - end of string.
See how this regex matches sample strings.

Related

Regex for 2-30 characters plus any alphanumeric characters separated by a single hyphen

I'm trying to come up with a regex for domain names that can either be 2-30 characters long with alphanumeric characters separated by a single hyphen with no other special characters allowed .
something like this thisi67satest-mydomain
What I have at the moment is this : /^[a-z0-9-]{2,30}$/ but this doesn't cover all scenarios especially with respect to the single hyphen.
I've always tried to google my way through these regexes. the above example will allow more than one hyphen which I don't want. How can i make the single hyphen mandatory?
Try this:
^(?=.{2,30}$)[a-z0-9]+-[a-z0-9]+$
^ the start of the line/string.
(?=.{2,30}$) ensures that the string between 2-30 characters.
[a-z0-9]+ one or more small letter or digit.
- one literal -.
[a-z0-9]+ one or more small letter or digit.
$ end of the line/string.
See regex demo
I think following pattern will work for you. Let me know if it work.
(\w|-(?!-)){2,30}

Using regex to find abbreviations

I am trying to create a regular expression that will identify possible abbreviations within a given string in Python. I am kind of new to RegEx and I am having difficulties creating an expression though I beleive it should be somewhat simple. The expression should pick up words that have two or more capitalised letter. The expression should also be able to pick up words where a dash have been used in-between and report the whole word (both before and after the dash). If numbers are also present they should also be reported with the word.
As such, it should pick up:
ABC, AbC, ABc, A-ABC, a-ABC, ABC-a, ABC123, ABC-123, 123-ABC.
I have already made the following expression: r'\b(?:[a-z]*[A-Z\-][a-z\d[^\]*]*){2,}'.
However this does also pick up these wrong words:
A-bc, a-b-c
I believe the problem is that it looks for either multiple capitalised letters or dashes. I wish for it to only give me words that have atleast two or more capitalised letters. I understand that it will also "mistakenly" take words as "Abc-Abc" but I don't believe there is a way to avoid these.
If a lookahead is supported and you don't want to match double -- you might use:
\b(?=(?:[a-z\d-]*[A-Z]){2})[A-Za-z\d]+(?:-[A-Za-z\d]+)*\b
Explanation
\b A word boundary
(?= Positive lookahead, assert that from the current location to the right is
(?:[a-z\d-]*[A-Z]){2} Match 2 times the optionally the allowed characters and an uppercase char A-Z
) Close the lookahead
[A-Za-z\d]+ match 1+ times the allowed characters without the hyphen
(?:-[A-Za-z\d]+)* Optionally repeat - and 1+ times the allowed characters
\b A word boundary
See a regex101 demo.
To also not not match when there are hyphens surrounding the characters you can use negative lookarounds asserting not a hyphen to the left or right.
\b(?<!-)(?=(?:[a-z\d-]*[A-Z]){2})[A-Za-z\d]+(?:-[A-Za-z\d]+)*\b(?!-)
See another regex demo.

Minimum letter constraint in regex pattern along with special characters

Currently, I am not expert in Regex, but I tried below thing I want to improve it better, can some one please help me?
Pattern can contain ASCII letters, spaces, commas, periods, ', . and - special characters, and there can be one digit at the end of string.
So, it's working well
/^[a-z ,.'-]+(\d{1})?$/i
But I want to put condition that at least 2 letters should be there, could you please tell me, how to achieve this and explain me bit as well, please?
Note that {1} is always redundant in any regex, please remove it to make the regex pattern more readable. (\d{1})? is equal to \d? and matches an optional digit.
Taking into account the string must start with a letter, you can use
/^(?:[a-z][ ,.'-]*){2,}\d?$/i
Details:
^ - start of string
(?: - start of a non-capturing group (it is used here as a container for a pattern sequence to quantify):
[a-z] - an ASCII letter
[ ,.'-]* - zero or more spaces, commas, dots, single quotation marks or hyphens
){2,} - end of group, repeat two or more ({2,}) times
\d? - an optional digit
$ - end of string
i - case insensitive matching is ON.
See the regex demo.
The thing to change in your regex is + after the list of allowed characters.
+ means one or many occurrences of the provided characters. If you want to have 2 or more you can use {2,}
So your regex should look something like
/^[a-z ,.'-]{2,}\d?$/i

Matching a lower-case character on the position before an uppercase character (camelCase)?

I have a regex in a piece of Typescript code that is used to match strings where there is a space, a dash/underscore or camelcase.
Because this pattern also is used to split the string later, in the case of the camelcase I actually need to match the lowercase character immediately before the camelcase/uppercase character, because I am trying to catch the camelcase. I am trying to reduce a string into two "initials" basically, so if I would input my alias for example "saddexProductions" or "Saddex Productions" etc, the output would be "SP". If there is no indicator that the string consists of two parts, for example "Saddexproductions", the output will be "Sa". If I match the uppercase character in the middle of the string though and split there, that character will be removed and the result with input "saddexProductions" would be "SR".
Here is what I have come up with so far:
const splitRegex: RegExp = /\s|(?<=.)([a-z](?<=[A-Z]{1}))|\-|\_/;
Specifically, it is this part that is relevant:
(?<=.)([a-z](?<=[A-Z]{1}))
All the other scenarios I have described but this one give the desired result. There can be pretty much anything in front and following the camelcase, but it is always the single lowercase character before the uppercase character that needs to be matched, not the uppercase character.
How would I accomplish this? Thanks in advance.
You can use
const splitRegex: RegExp = /[-_\s]|([a-z](?=[A-Z]))/;
Details:
[-_\s] - a character class matching a -, _ or a whitespace
| - or
([a-z](?=[A-Z])) - a capturing group with ID=1 that matches a lowercase ASCII letter followed with an uppercase ASCII letter without adding the latter to the overall match value (as it is inside a positive lookahead that is a non-consuming regex construct).

Regex to match 4 letters in a string

I am trying to write some regex that will match a string that contains 4 or more letters in it that are not necessarily in sequence.
The input string can have a mix of upper and lowercase letters, numbers, non-alpha chars etc, but I only want it to pass the regex test if it contains at least 4 upper or lowercase letters.
An example of what I would like to be a valid input can be seen below:
a124Gh0st
I have currently written this piece of regex:
(?(?=[a-zA-Z])([a-zA-Z])| )
Which returns 5 matches successfully but it will currently always pass as long as I have greater than 1 letter in the input string. if I add {4,} to the end of it then it works, but only in situations where there are 4 letters in a row.
I am using the following website to test what I have been doing: regex101
Any help on this would be greatly appreciated.
You may use
(?s)^([^a-zA-Z]*[A-Za-z]){4}.*
or
^([^a-zA-Z]*[A-Za-z]){4}[\s\S]*
See the regex demo.
Details:
^ - start of string
([^a-zA-Z]*[A-Za-z]){4} - exactly 4 sequences of:
[^a-zA-Z]* - 0+ chars other than ASCII letters
[A-Za-z] - an ASCII letter
[\S\s]* - any 0+ chars (same as .* if the DOTALL modifier is enabled).
Why don't you just match the zero or more characters between each letter? For example,
(?:[A-Za-z].*){4}
You'll recognize the [A-Za-z]. The . matches any character, so .* is a run of any number (including zero) of any character. The group of a letter followed by any number of any characters is repeated four times, so this pattern matches if and only if at least four letters appear in the string. (Note that the trailing .* of the fourth repeat of the pattern is mostly inconsequential, since it can match zero characters).
If you are using a regex language that supports reluctant quantifiers, then using them will make this pattern considerably more efficient. For example, in Java or Perl, one might prefer to use
(?:[A-Za-z].*?){4}
The .*? still matches any number of any character, but the matching algorithm will match as few characters as possible with each such run. This will reduce the amount of backtracking it needs to perform. For this particular pattern, it will reduce the needed backtracking to zero.
If you do not have reluctant quantifiers in your regex dialect, then you can achieve the same desirable effect a bit more verbosely:
(?:[A-Za-z][^A-Za-z]*?){4}
There, only non-letters are matched for the runs between letters.
Even with this, the pattern uses some regex features not present in all regex flavors -- non-capturing groups, enumerated quantifiers -- but these are present in your original regex. For a maximally-compatible form, you might write
[A-Za-z][^A-Za-z]*[A-Za-z][^A-Za-z]*[A-Za-z][^A-Za-z]*[A-Za-z]