I would like to know how to create a regex pattern in format:
111.222-3M
and
1112223M
What are numbers are only numbers allowed, and last one is a letter only.
Thx in advance.
A strict, case-sensitive version:
\d{3}\.?\d{3}-?\d[A-Z]
Will match 1112223M, 111.222-3M, 111.2223M and 111222-3M. If the last two are undesirable:
(\d{3}\d{3}\d[A-Z])|(\d{3}\.\d{3}-\d[A-Z])
Will match only the first two.
If those really are the only two patterns to match, this will work:
^\d+\.?\d+\-?\d+[a-zA-Z]$
As #ThomSmith stated, this regex will match numbers with the decimal and hyphen in different locations as well, such as 11.12-2223M. If that is undesirable, let me know.
EDIT:
If those patterns are exactly the same as the ones you want to match, this is a better one:
^\d{3}\.?\d{3}\-?\d[a-zA-Z]$
Related
I've tried a lot to split this string into something i can work with, however my experience isn't enough to reach the goal. Tried first 3 pages on google, which helped but still didn't give me an idea how to properly do this:
I have a string which looks like this:
My Dogs,213,220#Gallery,635,210#Screenshot,219,530#Good Morning,412,408#
The result should be:
MyDogs
213,229
Gallery
635,210
Screenshot
219,530
Good Morning
412,408
Anyone have an idea how to use regex to split the string like shown above?
Given the shared patterns, it seems you're looking for a regex like the following:
[A-Za-z ]+|\d+,\d+
It matches two patterns:
[A-Za-z ]+: any combination of letters and spaces
\d+,\d+: any combination of digits + a comma + any combination of digits
Check the demo here.
If you want a more strict regex, you can include the previous pattern between a lookbehind and a lookahead, so that you're sure that every match is preceeded by either a comma, a # or a start/end of string character.
(?<=^|,|#)([A-Za-z ]+|\d+,\d+)(?=,|#|$)
Check the demo here.
using the online word game Wordle (https://www.powerlanguage.co.uk/wordle/) to sharpen my Regex.
I could use a little help with something that I imagine Regex should solve easily.
given a 5 letter english word
given that I know the word begins with pr
given that I know that the letters outyase are not found in the word
given that I know that the letter i IS found in the word
what is the correct - most simplified regex?
my limited regex gives is this ^pr.[^outyase][^outyase]$ which is
a. redundant and
b. does not include the request to match i
any of you Regex Ninjas want to lend a hand, I would be much obliged.
by the way, the correct regex should return two nouns in the english language prick and primi, you can validate here https://www.visca.com/regexdict/
You may use this regex with a positive and negative lookahead conditions:
^pr(?=[a-z]*i)(?![a-z]*[outyase])[a-z]{3}$
Regex Explanation:
^: Start
pr: Match pr
(?=[a-z]*i): Positive lookahead to make sure we have an i ahead after 0 or more letters
(?![a-z]*[outyase])): Negative lookahead to disallow any of the [outyase] characters
[a-z]{3}: Match 3 letters
Demo Screenshot:
Trivially, you can use:
^pr([^outyase][^outyase]i|[^outyase]i[^outyase]|i[^outyase][^outyase])$
Also, according to your site, there's actually four words matching, not just two:
prick
primi
primp
prink
Try
^pr(?!.*[outyase])(?=.*i)[a-z]{3}$
(?!.*[outyase]) means don't match if any of outyase is found ahead in the string.
(?=.*i) means only match if there is an i ahead in the string.
Adding a note for general usage.
For any char position:
(?!.*[<BadChars>])(?=.*<firstGoodChar>)(?=.*<SecondGoodChar>)(?=.*<ThirdGoodChar>).*
If you know mghtlc are bad and i, o, and s are good:
^(?!.*[mghtlc])(?=.*i)(?=.*o)(?=.*s).*{5}$
It's trivial to add a pinned char at the front/back:
^b(?!.*[mghtlc])(?=.*i)(?=.*o)(?=.*s).*{4}$
^(?!.*[mghtlc])(?=.*i)(?=.*o)(?=.*s).*{4}n$
but I'm not sure how the look-ahead would work with a pinned char in the middle given that the found chars (i, o) can be on either side of the pinned s:
NOT WORKING:
^(?!.*[mghtlc])(?=.*i)(?=.*o).*{2}s(?!.*[mghtlc])(?=.*i)(?=.*o).*{2}$
R.replace(/[0-9](?!([0-9]{4}))/g,'*','123456789');
yields 12345****
want to input 123-45-6789 and yield 123-45-****
Currently based on above it yields ***-**-****
No idea why. I am using rambda js to simulate.
http://ramdajs.com/docs/#replace
need help to construct the regex for that . Any help is appreciated
Your current regex matches any digit that doesn't have four other digits immediately following it. Which is the case for every digit in the string 123-45-6789.
If the last four characters of the strings you are working with are always digits you could easily do this without a regex.
But if you want a simple regex, you could search with the following regex and replace with ****.
\d{4}$
Note that this regex wont match anything if the string doesn't end with four digits. So it would match the first three of the examples below and fail the last three.
12-345-6789
123-45-6789
123456789
1-2-3-4-5-6-7-8-9
12-34-56-78-9
123-456-789
If you want a regex that will work in all six cases you could use this:
\d(?=(?:\D*\d){0,3} *$)
R.replace(/\d(?=(?:\D*\d){0,3} $)/g,'','123-45-6789'); worked perfectly. Thanks heaps. Kudos to Francis !
is it possible with regex to match a particular sequence repeating it self rather than number of letters? I would like to be able to match cn.cn. or ti.ti. or xft.xft. but not vv.pp. or aa.bb. and I do not seam to be able to do that with (\w\w.)+ opposed to \w+.\w+. in the first case I want in fact to use only one occurrence, like cn. or ti. in the second I want to keep v.p. or a.b.
thanks for any help.
Depending on your flavor of regex, you can use backreferences in your regex to match an earlier group. Your question title and question body disagree, however, on what exactly is supposed to be matched. I'll answer in Python as that's the flavor I'm most familiar with.
# match vv.pp., no match cn.cn.
re.match(r"(\w)\1\.(\w)\2\.", some_text)
# match cn.cn., no match vv.pp.
re.match(r"(\w{2})\.\1\.", some_text)
I am struggling with a nice challenge to match two same numbers separately, with a regex.
See here the list I am trying to match separately.
1,680,000,0001,680,000,000
3,350,0003,350,000
110,000110,000
11,100,00011,100,000
550,000550,000
1,0001,000
250250
49,50049,500
165,000165,000
49,50049,500
3,350,0003,350,000
165,000165,000
550,000550,000
550,000550,000
33,10033,100
18,10018,100
450,000450,000
Take for example 550,000550,000, that's twice 550,000 or 250250 that's twice 250. I want to match for example 550,000 and 250.
I have tested many regular expressions in RegexBuddy, but no one does what I want. Maybe you have a suggestion?
If I understand your requirements correctly, then
^(.+)\1$
should work. You can restrict the possible matches to only allow digits and commas like this:
^([\d,]+)\1$
This matches a "double number" and keeps the first repetition in capturing group number 1. If you want your match only to contain the first repetition, then use
^([\d,]+)(?=\1$)