Regular expression that matches a single word in any order - javascript

After a long, unsuccessful search I am starting to wonder if what I am looking for is possible, I would like a regular expression which requires that each letter chosen is mandatory but only once and in any order.
Example : ^[abc]{3}$
The result I expect would be that it matches only that :
abc, bac, cba, acb
While I get :
acc, abb, cca, aab
Do you see where I am going with this?

You may use a regex like this with a negative lookahead of the matched character in a back-reference:
^(?:([abc])(?!.*\1)){3}$
RegEx Demo

Here is another way.
^(?!.*([abc]).*\1)[abc]{3}$
Demo
The negative lookahead
(?!.*([abc]).*\1)
asserts that no character is repeated and
[abc]{3}
together with the two anchors asserts that the string has a length of three and is composed of the characters in the character class.

Related

How to match one 'x' but not one or both of xs in 'xx' globally in string [duplicate]

Not quite sure how to go about this, but basically what I want to do is match a character, say a for example. In this case all of the following would not contain matches (i.e. I don't want to match them):
aa
aaa
fooaaxyz
Whereas the following would:
a (obviously)
fooaxyz (this would only match the letter a part)
My knowledge of RegEx is not great, so I am not even sure if this is possible. Basically what I want to do is match any single a that has any other non a character around it (except for the start and end of the string).
Basically what I want to do is match any single a that has any other non a character around it (except for the start and end of the string).
^[^\sa]*\Ka(?=[^\sa]*$)
DEMO
\K discards the previously matched characters and lookahead assertes whether a match is possibel or not. So the above matches only the letter a which satifies the conditions.
OR
a{2,}(*SKIP)(*F)|a
DEMO
You may use a combination of a lookbehind and a lookahead:
(?<!a)a(?!a)
See the regex demo and the regex graph:
Details
(?<!a) - a negative lookbehind that fails the match if, immediately to the left of the current location, there is a a char
a - an a char
(?!a) - a negative lookahead that fails the match if, immediately to the right of the current location, there is a a char.
You need two things:
a negated character class: [^a] (all except "a")
anchors (^ and $) to ensure that the limits of the string are reached (in other words, that the pattern matches the whole string and not only a substring):
Result:
^[^a]*a[^a]*$
Once you know there is only one "a", you can use the way you want to extract/replace/remove it depending of the language you use.

Find all char excluding group at the end with regexp

I have this string:
this is a test
at the end of this string I have a space and the new line.
I want to extract (for counting) all space group in the string witout the last space.
With my simple regex
/\s+/g
I obtain these groups:
this(1)is(2)a(3)test(4)
I want to exclude from group the forth space because i want to get only 3 groups if the string end with space.
What is the correct regexp?
Depending on the regex flavor, you can use two approaches.
If atomic groups/possessive quantifiers are not supported, use a lookahead solution like this:
(?:\s(?!\s*$))+
See the regex demo
The main point is that we only match a whitespace that is not followed with 0+ other whitespace symbols followed with an end of string (the check if performed with the (?!\s*$) lookahead).
Else, use
\s++(?!$)
See another demo. An equivalent expression with an atomic groups is (?>\s+)(?!$).
Here, we check for the end of string position ONLY after grabbing all whitespaces without backtracking into the \s++ pattern (so, if after the last space there is an end of string, the whole match is failed).
Also, it is possible to emulate an atomic group in JavaScript with the help of capturing inside the positive lookahead and then using a backreference like
(?=(\s+))\1(?!$)
However, this pattern is costly in terms of performance.

regexp to match characters not flowed by a character that is repeated an amount of times

i built this regexp to match letters and numbers and dots
dots only if they are not repeated successfully
example :
something.somethnElse.another.then.something
this is a match because the dots are separated.
but in the following case :
something..thensomething
is no match because there is one or more dots next to eachother
this is my regexp, recommend me please why it's not working
[a-zA-Z0-9\.]+(?!\.{2,})
i tried also
[a-zA-Z0-9\.]+(?![\.]+)
but they both give a match for successful dots
You are close. You can use this regex:
^(?!.*?\.{2})[a-zA-Z0-9.]+$
RegEx Demo
PS: No need to escape the dot inside character class
First of all, the expression should be anchored, otherwise it only requires a very minimal match.
Additionally, you could think of your expression as a chain of letters and digits that can be interrupted by exactly one dot.
So:
/^(?:[a-z0-9]+|\.(?!\.))*$/
You need to use beginning of string ^ and end of string $ anchors and place the lookahead at the beginning.
/^(?!.*\.{2})[a-z0-9.]+$/i
Live Demo

JS regular expression for repeated unique character

I am trying to determine whether a string contains exactly one given distinct character, repeated at least once. For example "bbbbb" should match but "ccMMcc" should not as it contains two different letters, c and m respectively. Presuming regular expressions would be the simplest way to do so, shame I suck at them, what will I need to match my string against?
You can use a backreference:
^(.)\1+$
The regex is:
\b(\w)\1*\b
that is:
\b: word boundary
(\w): the first char, group
\1*: any number of repetition of the first char
"tttt".match(/^(.)\1*$/) returns ["tttt","t"] but "test".match(/^(.)\1*$/) returns null

Regular Expression form {m,n} does not use upper limit

My understanding was that the regexp form a{m,n} would match a at most n times. However, the following snippet does not work as I would expect (this is javascript):
/\{{2,2}/.exec ('df{{{df')
// [ '{{', index: 2, input: 'df{{{df' ]
Shouldn't it return null?
It is matching the text because there are two. That satisfies the requirements your regex specifies. If you want to prevent extras from matching use a negative lookahead: (?!\{).
(?:^|[^{])(\{{2,2}(?!\{))
Then, use the first captured group.
Edit, by the way, the the ,2 in {2,2} is optional in this case, since it's the same number.
Edit: Added usage example to get rid of first matched character. (Javascript doesn't support negative lookbehind.
var myRegexp = /(?:^|[^{])(\{{2,2}(?!\{))/g;
var match = myRegexp.exec(myString);
alert(match[1]);
What your expression states is find {{ anywhere in the string, which it will find. If you want to find only {{ and not {{{ then you need to specify that you want to find:
/[^{]\{{2,2}[^{]/
In English:
[Any Character Not a {] followed by [Exactly 2 {] followed by [Any Character Not a {]
This will match a{{b but not a{b and not a{{{{b
It matches because it contains a substring with exactly 2 left braces. If you want it to fail to match, you have to specify that anything outside the 2 left braces you are looking for can't be a left brace.
That regular expression is looking for exactly two left-curly-braces ({{), which it finds in the string "df{{{df" at index 2 (immediately after the first "df"). Looks right to me.

Categories

Resources