regex: match any word with numbers - regex

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

Related

Regex to match only the following strings

I have a few strings and I need some help with constructing Regex to match them.
The example strings are:
AAPL10.XX1.XX2
AAA34CL
AAXL23.XLF2
AAPL
I have tried few expressions but couldn't achieve exact results. They are of the following:
[0-9A-Z]+\.?[0-9A-Z]$
[A-Z0-9]*\.?[^.]$
Following are some of the points which should be maintained:
The pattern should only contain capital letters and digits and no small letters are allowed.
The '.' in the middle of the text is optional. And the maximum number of times it can appear is only 2.
It should not have any special characters at the end.
Please ask me for any clarification.
You can write the pattern as:
^[A-Z\d]+(?:\.[A-Z\d]+){0,2}$
The pattern matches:
^ Start of string
[A-Z\d]+ Match 1+ chars A-Z or a digit
(?:\.[A-Z\d]+){0,2} Repeat 0 - 2 times a . and 1+ chars A-Z or a digit
$ End of string
Regex demo

Regex - match behind an optional character

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.

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

match only letters after comma without numbers

im using regex to match certain text after selecting with xpath
for example Huntsville, Alabama 11111
i want only Alabama which always come after comma
and i use [^,]*$ to get text after comma
but i can't seem to find a way to exclude numbers or returns only the letters
another exmaple when i want to get the numbers after the comma i use [^[0-9],]*$
but when i tried to tweak it with anything else it only return numbers or nothing.
[?<=,\s*][a-zA-Z]+ You can try this.
Explanation:
?<= => lookbehind to match a string but not include in capture group
,\s* => match comma followed by 0 or more spaces
[a-zA-Z]+ => match letters only (one or more)
HTH
To match a letter word after the last comma, you may use
[a-zA-Z]+(?=[^,]*$)
See the regex demo.
Details
[a-zA-Z]+ - 1 or more ASCII letters
(?=[^,]*$) - followed with 0+ chars other than , up to the end of the string.
To match 1 or more words in the same context, use
[a-zA-Z]+(?:\s+[a-zA-Z]+)*(?=[^,]*$)
^^^^^^^^^^^^^^^^^
See this regex demo.
The (?:\s+[a-zA-Z]+)* part matches zero or more consequent occurrences of 1+ whitespaces and 1+ ASCII letters.