Regex - match behind an optional character - regex

I want to match a string that has more than 3 characters and combine with positive look behind an optional character (/).
From the input:
100/ABC-12345 10
ABCD
ZZZ
I need to retrieve:
ABC-12345 10
ABCD
I can match them separately but cannot combine them. See my current regex:
(?<=\/).*

You may use:
(?<=/|^)\w[\w-]{3,}
Updated Regex Demo
Positive lookbehind (?<=/|^) asserts presence of / or start of line behind current position and \w[\w-]{3,} matches at least 4 of word or hyphen characters where first character must be a word character.

Try this:
/?[\w-]{4,}
See live demo.
This matches a slash optionally, then at least 4 of word chars or dashes.

Related

Regex: match only last 2 digits and ignore whitespaces at the end of line

Example:
32-12•
32-12•••
32-12-52••
32-12-53-12
(let's say Bullet Point "•" is Whitespaces)
What I have tried is
/(?<=^.*)\d{2}(?= *)$/gm
but it seem like it does match only last 2 digits that whitespaces doesn't concat like this
32-12•
32-12•••
32-12-52••
32-12-53-12
(let's say bold strings are where regex matched)
but what I want is last 2 digits ignore whitespaces like this
32-12•
32-12•••
32-12-52••
32-12-53-12
You can use
\d{2}(?= *$)
See the regex demo. To match any whitespaces, replace the literal space with as \s shorthand character class: \d{2}(?=\s*$).
Details:
\d{2} - two digits
(?= *$) - a positive lookahead that requires zero or more chars and the end of string position to appear immediately to the right of the current location.

Regex for finding words containing more then 3 'a' characters

I need to write a regex that will find all words with 3 or more 'a' letters. Suppose that each word is on a new line.
Example of correct words:
Anagram
Assassination
Abaca
I end up with something like this:
^([^aA]*a[^aA]*a[^aA]*a)$
But it will not work correctly if there will be more than 3 'a' letters or if word starts with 'a'.
I would keep it simple and just use:
\b\w*[Aa]\w*[Aa]\w*[Aa]\w*\b
Demo
This regex pattern matches any word containing three lower/upper a/A characters in it, appearing anywhere in the word.
Here is what I tried:
^(?i)(?:[b-z]*a){3}[a-z]*$
See an online demo
^ - Start line anchor.
(?i) - Match rest case-insensitive.
(?:[b-z]*a){3} - A non-capture group where you would match 0+ characters ranging from b-z upto a literal "a". Repeated three times.
[a-z]* - Match any possible remainder.
$- End line anchor.
If you want to use the anchors, you can add matching .* at the end, and add \n to the negated character class to prevent crossing newlines.
^[^aA\n]*[aA][^aA\n]*[aA][^aA\n]*[aA].*$
Regex demo
Or a bit shorter
^(?:[^aA\n]*[aA]){3}.*$
Regex demo

What is the Regex pattern "words with numbers in them" but not a number itself

How does a regex look like for
Input:
Rood Li-Ion 12 G6
Match:
"Rood" "Li-Ion" "G6"
1.
I tried
\b[\w-]+\b /g
But that matches the "12" also!
2.I tried
/([0-9]+)?[a-zA-Zê]/
But that didn't match G6.
I want all words even if they have a number in them but I dont want only numbers to match. How is this possible. Whitespace also shall not be part of the match.
"Rood Li-Ion 12 G6" shall become 3 strings of "Rood","Li-Ion","G6"
You can use
(?<!\S)(?!\d+(?!\S))\w+(?:-\w+)*(?!\S)
See the regex demo. It matches strings between whitespaces or start/end of string, and only when this non-whitespace chunk is not a digit only chunk.
Also, it won't match a streak of hyphens as your original regex.
Details
(?<!\S) - a left whitespace boundary
(?!\d+(?!\S)) - no one or more digits immediately to the right capped with whitespace or end of string is allowed
\w+(?:-\w+)* - one or more word chars followed with zero or more repetitions of - and one or more word chars
(?!\S) - a right whitespace boundary
This should suit your needs:
\b[\w-]*[a-zA-Z][\w-]*\b

how to match a list of fixed length words separated by space or comma?

The words' length could be 2 or 6-10 and could be separated by space or comma. The word only include alphabet, not case sensitive.
Here is the groups of words that should be matched:
RE,re,rereRE
Not matching groups:
RE,rere,rel
RE,RERE
Here is the pattern that I have tried
((([a-zA-Z]{2})|([a-zA-Z]{6,10}))(,|\s+)?)
But unfortunately this pattern can match string like this: RE,RERE
Look like the word boundary has not been set.
You could match chars a-z either 2 or 6 - 10 times using an alternation
Then repeat that pattern 0+ times preceded by a comma or a space [ ,].
^(?:[A-Za-z]{6,10}|[A-Za-z]{2})(?:[, ](?:[A-Za-z]{6,10}|[A-Za-z]{2}))*$
Explanation
^ Start of string
(?:[A-Za-z]{6,10}|[A-Za-z]{2}) Match chars a-z 6 -10 or 2 times
(?: Non capturing group
[, ](?:[A-Za-z]{6,10}|[A-Za-z]{2}) Match comma or space and repeat previous pattern
)* Close non capturing group and repeat 0+ times
$ End of string
Regex demo
If lookarounds are supported, you might also assert what is directly on the left and on the right is not a non whitespace character \S.
(?<!\S)(?:[A-Za-z]{6,10}|[A-Za-z]{2})(?:[ ,](?:[A-Za-z]{6,10}|[A-Za-z]{2}))*(?!\S)
Regex demo
([a-zA-Z]{2}(,|\s)|[a-zA-Z]{6,10}|(,|\s))
This one will get only the words who have 2 letter, or between 6 and 10
\b,?([a-zA-Z]{6,10}|[a-zA-Z]{2}),?\b
You can use this
^(?!.*\b[a-z]{4}\b)(?:(?:[a-z]{2}|[a-z]{6,10})(?:,|[ ]+)?)+$
Regex Demo
This regex will match your first case, but neither of your two other cases:
^((([a-zA-Z]{2})|([a-zA-Z]{6,10}))(,|[ ]+|$))+$
I'm making the assumption here that each line should be a single match.
Here it is in action.

regex: match any word with numbers

I checked on stackoverflow already but didn't find a solution I could use.
I need a regular expression to match any word (by word I mean anything between full spaces) that contains numbers. It can be alphanumeric AB12354KFJKL, or dates 11/01/2014, or numbers with hyphens in the middle, 123-489-568, or just plain normal numbers 123456789 - but it can't match anything without numbers.
Thanks,
Better example of what I want (in bold) in a sample text:
ABC1 ABC 23-4787 ABCD 4578 ABCD 11/01/2014 ABREKF
There must be something better, but I think this should work:
\S*\d+\S*
\S* - Zero or more non-whitespace characters
\d+ - One or more digits
\S* - Zero or more non-whitespace characters
Use this lookahead:
(?=\D*\d)
This asserts that the string contains any quantity of non numeric characters (\D) followed by a single digit.
If you want to match/capture the string, then just add .* to the regex:
(?=\D*\d).*
Reference: http://www.rexegg.com/regex-lookarounds.html