Regex matching first part of domain name - javascript

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.

Related

How to not match given prefix in RegEx without negative lookbehind?

Goal
The goal is matching a string in JavaScript without certain delimiters, i.e. a string between two characters (the characters can be included in the match).
For example, this string should be fully matched: $ test string $. This can appear anywhere in a string. That would be trivial, however, we want to allow escaping the syntax, e.g. The price is 5\$ to 10\$.
Summarized:
Match any string that is enclosed by two $ signs.
Do not match it if the dollar signs are escaped using \$.
Solution using negative lookbehind
A solution that achieves this goal perfectly is: (?<!\\)\$(.*?)(?<!\\)\$.
Problem
This solution uses negative lookbehind, which is not supported on Safari. How can the same matches be achieved without using negative lookbehind (i.e. on Safari)?
A solution that partially works is (?<!\\)\$(.*?)(?<!\\)\$. However, this will also match the character in front of the $ sign if it is not a \.
You might rule out what you don't want by matching it, and capture what you want to keep in group 1
\\\$.*?\$|\$.*?\\\$|(\$.*?\$)
Regex demo
You may use this regex and grab your inner text using capture group #1 as you are already doing in your current regex using lookbehind:
(?:^|[^\\])\$((?:\\.|[^$])*)\$
RegEx Demo
RegEx Details:
(?:^|[^\\]): Match start position or a non-backslash character in a non-capturing group
\$: Match starting $
(: Start capturing group
(?:\\.|[^$])*: Match any escaped character or a non-$ character. Repeat this group 0 or more times
): End capturing group
\$: Match closing $
PS: This regex will give same matches as your current regex: (?<!\\)\$(.*?)(?<!\\)\$

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.

Grab full regex word if pattern inside it matches

How do I retrieve an entire word that has a specific portion of it that matches a regex?
For example, I have the below text.
Using ^.[\.\?\!:;,]{2,} , I match the first 3, but not the last. The last should be matched as well, but $ doesn't seem to produce anything.
a!!!!!!
n.......
c..,;,;,,
huhuhu..
I want to get all strings that have an occurrence of certain characters equal to or more than twice. I produced the aforementioned regex, but on Rubular it only matches the characters themselves, not the entire string. Using ^ and $
I've read a few stackoverflow posts similar, but not quite what I'm looking for.
Change your regex to:
/^.*[.?!:;,]{2,}/gm
i.e. match 0 more character before 2 of those special characters.
RegEx Demo
If I understand well you are trying to match an entire string that contains at least the same punctuation character two times:
^.*?([.?!:;,])\1.*
Note: if your string has newline characters, change .* to [\s\S]*
The trick is here:
([.?!:;,]) # captures the punct character in group 1
\1 # refers to the character captured in group 1

RegEx which starts with a letter and contains x characters in 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.

Search validation regex not working in JavaScript

I want to validate my textbox with a regex. The textbox's value should be like this – "/word/Search" – i.e. forward slash, then any word, then forward slash again, then the fixed word "Search".
Here is an example input:
/admin/Search
I tried the following pattern, but it's not matching:
[/\/][\w][/\/][\Search]
What is wrong in this pattern?
Try this instead:
\/\w+\/Search
\/ – no real need for a character class (i.e. opened by [ and closed by ]) to match a slash...when you can just match the slash (Note that \/ presumes you are using a regex literal rather than constructing a RegExp object: for the latter / would do the trick.)
\w+ – one or more word characters (similarly with no need for a character class)
\/ – the closing slash (same as at the outset)
Search – the fixed word Search that you expect following the closing slash (also with no need for a character class)
You can try the updated pattern against the sample textbox input you provided in a regex fiddle.
(Nitpicky) Caveat:
You may want to tighten up the word subpattern (i.e. \w+) because \w...
Matches any alphanumeric character including the underscore. Equivalent to [A-Za-z0-9_].
...according to MDN's JavaScript regular expressions reference. At least be conscious that it matches digits and underscore.
If you do want to tighten it up, then you have an occasion for a character class – for example:
[A-Za-z0-9]+ – like \w+ except that it will not match underscores
[A-Za-z]+ – like \w+ except that it will not match digits or underscores
Since everyone seems to ignore your actual question, what's wrong with your regex is this:
character classes [...]. This makes [\Search] match a single character out of \Search, not the literal word Search.
for some reason you wrote /\/ when you actually just want to match a single forward slash /.
You want to match multiple characters, so you have to change \w to \w+.
Result:
/\w+/Search
I think this is the expresion you are looking for:
\\[\w]*\\Search

Categories