RegEx which starts with a letter and contains x characters in javascript - javascript

Is there a regular expression pattern in JavaScript which searches for strings which starts with a letter (e.g. letter B) and consists of fixed number of characters (e.g. 8)?
I have tried a lot of variations with ^B followed by [A-Za-z]{7}, but nothing worked out.
UPDATE:
As a final solution an alternative version of #stribizhev's answer worked for me. As I was filtering object attributes in a relational DB style, I had to match the exact string without returning records with multiple words starting with the matching string and separated by whitespaces.
The RegEx \bB/S{7}$\b worked, as a record can contain special characters, and the whitespace character acts as the word separator, as in any human-friendly table.

\bB\w{7}\b is a pattern for any word starting with B and that has 8 characters. Have a look at https://regex101.com/r/tF3aA5/1.
The word boundary \b enables the whole word matching.

Related

RegEx matching help: won't match on each appearence

I need to write a little RegEx matcher which will match any occurrence of strings in the form of
[a-zA-Z]+(_[a-zA-Z0-9]+)?
If I use the regex above it does match the sections needed but would also match onto the abc part of 4_abc which is not intended. I tried to exclude it with:
(?:[^a-zA-Z0-9_]|^)([a-zA-Z]+(_[a-zA-Z0-9]+)?)(?:[^a-zA-Z0-9_]|$)
The problem is that the 'not' matches at the beginning and end are not really working like I hoped they would. If I use them on the example
a_d Dd_da 4_d d_4
they would block matching the second Dd_da because the space was used in the first match.Sadly I can't use lookarounds because I am using JS.
So the input:
a_d Dd_da 4_d d_4
should match: a_d, Dd_da and d_4
but matches: a_d (there is a space at the end)
Is there another way to match the needed sections, or to not consume the 'anchor' matches?
I really appreciate your help.
You can make use of \b:
\b[a-zA-Z]+(_[a-zA-Z0-9]+)?\b
\b matches the (zero-width) point where either the preceding character or following character is a letter, digit or underscore, but not both. It also matches with the start/end of the string if the first/last character is a letter, digit or underscore.

Regex - Allow capital letters just at the beginning of words

I have to check for capital letters to exist just at the beginning of words.
My regex now looks like this:
/^([A-ZÁÉÚŐÓÜÖÍ]([a-záéúőóüöí]*\s?))+$/
It's at the words beginning works good, but if the problem not at the beginning of the word it's fails.
For example: John JohnJ got validated.
What should i alternate in my regex to works well?
In your regex pattern the space is optional, allowing combinations like JJohn or JohnJ - the key is to make it required between words. There are two ways to do this:
Roll out your pattern:
/^[A-ZÁÉÚŐÓÜÖÍ][a-záéúőóüöí]*(?:\s[A-ZÁÉÚŐÓÜÖÍ][a-záéúőóüöí]*)*$/
Or make the space in your pattern required, but alternatively allow it to be the end of line (this allows a trailing space though).
/^(?:[A-ZÁÉÚŐÓÜÖÍ][a-záéúőóüöí]*(?:\s|$))+$/
In both patterns I have removed some superfluous groups of your original and turned all groups into non-capturing ones.
You can do this: /^([A-ZÁÉÚŐÓÜÖÍ]{0,1}([a-záéúőóüöí]*\s?))+$/
With {a,b}, a is the least amount of characters it will match, whereas b is the most amount of characters it will match.
If there is ALWAYS going to be a capital letter at the beginning, instead you can simply use: /^([A-ZÁÉÚŐÓÜÖÍ]{1}([a-záéúőóüöí]*\s?))+$/
In this preceding case, {c}, c is the exact number of characters it will match.
Here is a resource with good information.

Regex matching first part of domain name

I am trying to match regex for the Fully Qualified Domain Name (fqdn) like this:
1.test02a-d.test.example.net
2.test02b-d.test.example.net
3.test03a.test.example.net
4.test03b.test.example.net
5.test04-d.test.example.net
6.test05.test.example.net
I tried this regex to get the first part of the fqdn:
[^.]+
Criteria:
Here I need to get the list of fqdn that has alphabets 'a' or 'b' immediately after number in first part. As shown in the above example 1,2,3,4 matches this condition.
i.e., test02a, test02a-d, test 02b and test02b-d
Fqdn 5 and 6 should be ignored as per the above criteria.
How to modify regex for matching this criteria?
Note: This should be used as REGEXP in Mysql and hence some direct javascript regexes didn't work. The solution should be applicable for both javascript and mysql.
MySQL version:
^[^.]+[0-9]+[ab](-[^.]+)?[[:>:]]
JavaScript version:
^[^.]+[0-9]+[ab](-[^.]+)?\b
regex101.com doesn't support MySQL flavor regexp, so I only give the JavaScript version. test1bw.test.example.net is added as a test string.
[[:>]] is specific to MySQL. For JavaScript, \b should be used instead of [[:>]]. [[:>]] is a Zero-Length Assertion matching at the end of a word (position preceded by but not followed by an ASCII letter, digit, or underscore). \b matches at a position that is the start or end of a word.
MySQL doesn't support any shorthand character classes, so [[:digit:]] or [0-9] should be used instead of \d
[a-z]+[0-9]+[ab](-d){0,1}(.test.example.net)
This Regex matches your first four domain names. The "[a-z]+" could also be specified with "(test)"
You can test it on this site: http://regexr.com/
Try
^[^.]+\d[ab][^.]*
It'll match, from the start of a line (^) any characters (more than one) not being a dot. Then there must be a digit followed by an a or a b, Optionally followed by another sequence of characters not being a dot.
See it here at regex101.
[a-zA-Z]+[\d]+[a-zA-Z]+.*
workes for me... try it here matches the whole domain.
[a-zA-Z]+[\d]+[a-zA-Z]+[-\w]*
matches only the first part of the domain.

Match Strings Composed of Substrings in a Given Set

By searching a dictionary of words, I am trying find strings made up of substrings.
First, finding string made of letters is straight forward:
1. [abcdefgjlmnqrsz]+
Above finds any word or phrase that contains the above letters. What I am trying to figure out how to find strings made up of substrings:
So for example dictionary = ["neon","none","dog","bear","bare"]
and regex is: [ar|be|o|ne|n]+
I would like to find: neon, bear
But, regex ex. 2 is incorrect, because it finds: neon, noen, bear, bare.
Any help appreciated
A Character Class Matches One Single Character
You are looking for
\b(?:ar|be|o|ne|n)+\b
Note that [things] is a character class that matches one single character. Therefore [ar|be|o|ne|n] does not mean what you thought: it means "one character that is either one of a,r,|,b,e,|,o,|,n,e,|,n
Explanation
\b is a word boundary that matches a position where one side is a letter, and the other side is not a letter (for instance a space character, or the beginning of the string)
(?: ... ) is a non-capture group
| is the alternation (OR) operator

RegExp to match hashtag at the begining of the string or after a space

I have looked through previous questions and answers, however they do not solve the following:
https://stackoverflow.com/questions/ask#notHashTag
The closest I got to is this: (^#|(?:\s)#)(\w+), which finds the hashtag in half the necessary cases and also includes the leading space in the returned text. Here are all the cases that need to be matched:
#hashtag
a #hashtag
a #hashtag world
cool.#hashtag
##hashtag, but only until the comma and starting at second hash
#hashtag#hashtag two separate matches
And these should be skipped:
https://stackoverflow.com/questions/ask#notHashTag
Word#notHashTag
#ab is too short to be a hashtag, 3 characters minimum
This should work for everything but #hashtag#duplicates, and because JS doesn't support lookbehind, that's probably not possible to match that by itself.
\B#\w{3,}
\B is designed to match only between two word characters or two non-word characters. Since # is a non-word character, this forces the match to be preceded by a space or punctuation, or the beginning of the string.
Try this regex:
(?:^|[\s.])(#+\w{3,})(#+\w{3,})?
Online Demo: http://regex101.com/r/kG1nD5

Categories