regex expression to find words with 0 - regex

Please provide a regex expression to find words with 0 like 'A0lytics', 'Alter0tive Medicine'.
But, it should not match words like 'Enterprise 2.0'

If you have this list of words: ["A0lytics", "0tics", "Alter0tive Medicine", "Enterprise 2.0"]
\w+0\w+ will capture "A0lytics" and "Alter0tive" (not "Alter0tive Medicine")
^[A-Za-z][A-Za-z0]+(?: +[A-Za-z][A-Za-z0]+)*$ will match "A0lytics" and "Alter0tive Medicine"
(?<!\.)([a-zA-Z]*)0([a-zA-Z]*) will match "A0lytics", "0tics" and "Alter0tive Medicine" (if you can use the negative lookbehind).
Good luck.

You can use:
^[A-Za-z][A-Za-z0]+(?: +[A-Za-z][A-Za-z0]+)*$
Demo: https://regex101.com/r/gJ5ugU/2

\w+0\w+
is it useful?
You can test online regular expressions with this.

Related

Regular expression ((MM)*N{1,3})|((N{1,3})(MM)*) not matching what I think it should

I am trying to write a regular expression that would match pairs of Ms and up to 3 Ns consecutively in any order so
MMMMNN would match
MMNNN would match
NNNMM would match
NMMMM would also match
I used the following regular expression:
((MM)*N{1,3})|(N{1,3}(MM)*)
each term matches alone but when I put the | between them it doesn't seem to match both possibilities. I used http://regex101.com/ to test it.
What regular expression would match those?
This matches all the examples you have:
(N{1,3}(MM)+)|((MM)+N{1,3})
The question is however, if 'up to 3' should include zero instances?
Edit: The comment is correct, removed the extra plus.
Does this work for you...
(MMN{0,3})|(N{0,3}MM)

How to build this regular expression?

Sample: AAAATGCCCTAAGGGATGTTTTAGAAA
I want to capture all string with these criteria:
Start: ATG
Follow by 3x characters of sets: A or C or G or T
End: TAA or TAG or TGA
Such as: ATGCCCTAA, ATGTTTTAG
I had a regular expression here: /[ACGT]*((ATG)(([ACGT]){3})+(TAA|TAG|TGA))[ACGT]*/g, but it only match the last ATGTTTTAG not ATGCCCTAA. I don't know why ?
Please help me write pattern that match both ATGCCCTAA and ATGTTTTAG.
Here is online example:
https://regex101.com/r/iO8lF9/1
This regex works well /(ATG(?:A{3}|C{3}|G{3}|T{3})(?:TAA|TAG|TGA))/g
as you can see here: https://www.regex101.com/r/gZ0zA9/2
I hope it helps
Using back-reference you can shorten your regex as this:
ATG([AGCT])\1{2}(?:TGA|TA[AG])
RegEx Demo
It matches [AGCT] after ATG and groups it as captured group #1. Next we match \1{2} to make sure same letter is repeated 3 times.
try...
^ATG[AGCT]{3}(TAA|TAG|TGA)$
I use this pattern and it works, thank all you for helping me.
/(ATG(:?A{3}|C{3}|G{3}|T{3})(:?TAA|TAG|TGA))/g

Regular expression to match particular starting word or nothing

I'm struggling to come up with the correct regex for the following scenario.
Let's say you have to match a word either starts with http- or nothing
eg : http-test-data, test-data should be a match but xyz-test-data shouldn't be a match
the regex i came up so far is
(?:http-)?(test-data)
but it matches xyz-test-data as well.
You could simply use the following:
(?:http-|^)(test-data)
This tests for either a positive look-behind of http- or for the beginning of the string before test-data.
For example, for the sample data as follows:
http-test-data
xyz-test-data
http-test-data
xyz-test-data
test-data
yes-yes-test-data
-test-data
It yeilds:
http-test-data
http-test-data
test-data
Try this representation
^(http-|)(test-data)
Yes because there is a ? on the (?:http-). Then the regex will also match any string that contains test-data.

php regex to match three words if not then two and then one

Q1: I'm writing a regex in php and not successful. I want to match the following:
so i would
if not then match:
so i
and then:
i would
and
so
i
would
Here is my code:
\b(so i|i would|so i would|(so|i|would))\b
Its only matching the: so, i, would, so i, i would .... but not matching the so i would?
Order your regex correctly.
\b(so i would|so i|i would|(so|i|would))\b
Put the longest string to match to the left.
The | is left-associative and hence, in your version Of the regex, is matching the shorter string.
Just put it at the beginning
\b(so i would|so i|i would|(so|i|would))\b
put longest pattern to left in the group: \b(long|...|short)\b
another solution: \b(so i would|i would|would|so i|so|i)\b
p.s. this is NFA regex engine feature, please refer to "Mastering Regular Expressions"

Regexp help: how can I detect if a string looks like "say [any number] [anything]"

Without the []'s. I'd appreciate any help.
For integers:
/^say \d+.*$/
But you might also want floating point:
/^say (\d+\.)?\d+.*$/
Based on the limited information presented a Perl Regular expression will look like /^say \d.*$/ where \d matches the "number" and .* matches "anything".
POSIX regex:
say [[:digit:]]+.*
Perl regex:
/say \d+.*/
Regexes like those are fun. If your 'any number' is guaranteed to be an integer, you could do this:
/say \d+/
If you wanted to capture the number and the anything, you could do this:
/say (\d+) (.*)/
..which saves them using "groups".