Regular expression to match exact number of characters?
What you have is correct, but this is more consice:
^[A-Z]{3}$
Regex match exact number of a specific character
You may use:
^(?:[^e]*e){2}[^e]*$
See the regex demo. The (?:...)
is a non-capturing group that allows quantifying a sequence of subpatterns and is thus easily adjustable to match 3, 4 or more specific sequences in a string.
Details
^
- start of string(?:[^e]*e){2}
- 2 occurrences of[^e]*
- any 0+ chars other thane
e
- ane
[^e]*
- any 0+ chars other thane
$
- end of string
See the R demo below:
x <- c("feel", "agre", "degree")
rx <- "^(?:[^e]*e){2}[^e]*$"
grep(rx, x, value = TRUE)
## => [1] "feel"
Note that instead of value = T
it is safer to use value = TRUE
as T
might be redefined in the code above.
Regex to match exact number of occurence
If you want to detect {{ variable }
as valid and ignore {{ variable }}
You have to verify the end of the string using $
Regex Needed /{{[^}]+}$/g
Explanation
Visulization
const input1 = "{{ variable }";
console.log(input1.match(/{{[^}]+}$/g));
const input2 = "{{ variable }}";
console.log(input2.match(/{{[^}]+}$/g));
Regex Match string having exact number of a char
With N being the number you search for:
"^([^:]*:){N}[^:]*$"
Here is a test:
for s in ":::foo:bar" "foo:bar:::" "fo:o:ba::r" ; do echo "$s" | egrep "^([^:]*:){4}[^:]*$" ; done
Change 4 to 3 and 5, to see it not matching.
Maybe postgresql needs specific flags or masking for some elements.
"^([^:]*:){N}[^:]*$"
"^ $"
# matching the whole String/Line, not just a part
"([^:]*:){N}[^:]*"
"( ){N}[^:]*"
# N repetitions of something, followed by an arbitrary number of non-colons (maybe zero)
"([^:]*:)"
# non-colons in arbitrary number (including zero), followed by a colon
Regex to match string with exact number of identifier characters
I suggest using
string.match(/(?<!\*)\*{2}[^*]*\*{2}(?!\*)/g)
See the regex demo
Pattern details
(?<!\*)
- a negative lookbehind that fails the match if there is an asterisk immediately to the left of the current location\*{2}
- double asterisk[^*]*
- zero or more chars other than an asterisk\*{2}
- double asterisk(?!\*)
- a negative lookahead that fails the match if there is an asterisk immediately to the right of the current location
JavaScript demo:
const string = "#### And this will not work!\nThis is an *italic*... does it work!\nThis is a **bold** text and it will work!\nWill this ***work***";
console.log(string.match(/(?<!\*)\*{2}[^*]*\*{2}(?!\*)/g));
Regex to match exact number
Brief
You want to use the Find and Replace
window found at Edit -> Find and Replace -> Find in Files
with the regex \b1[23]\b
and the Find Options
Use Regular Expressions
checkbox selected.
Code
\b
Word boundary assertion- Matches, without consuming any characters, immediately between a character matched by
\w
and a character not matched by\w
(in either order). It cannot be used to separate non-words from words.
- Matches, without consuming any characters, immediately between a character matched by
1
Match this literally[23]
Match a character in the set (2
or3
)\b
Word boundary assertion
RegEx R: match strings with same character exact number of times anywhere in string
What you're missing is the negated character class. You want to match something that is not an underscore and then an underscore. It's generally like [^X]
.
/^[^_]*_[^_]*_[^_]*$/
# or
/^(?:[^_]*_){2}[^_]*$/
That is:
beginning of string
anything not an underscore
underscore
anything not an underscore
underscore
anything not an underscore
end of string
This is just one way to do it.
HTH
Regular Expression to Match Exact Character and Numbers
Remove the outer square brackets:
if(name.matches("[K][H][1-9][0-9]?")){
See IDEONE demo
The issue is that you enclosed the whole pattern into a character class with outer [...]
, and all the symbols inside (except the brackets) were treated as single literal symbols, and the whole expression could only match 1 character.
Talking about optimizations: the alternation is not really necessary here since you can apply ?
quantifier to the [0-9]
class to make it optional. ?
matches 0 or 1 occurrence of the preceding subpattern.
Also note that [K][H]
makes sense if you plan to add more options into the character classes, otherwise you might as well use
if(name.matches("KH[1-9][0-9]?")){
or
if(name.matches("KH[1-9]\\d?")){
The \d
is a shorthand class that matches digit(s).
How to to match exact number in regex?
This would be the regex that you would be looking for,
^\+380[0-9]{9}$
This regex ensures that you start with +380
.
It also ensures that there are 12 digits, inclusive of 380
, after +
.
Find the explanation here, https://regex101.com/r/UDjaZV/2/
Related Topics
Doing a Plyr Operation on Every Row of a Data Frame in R
Find the Index Position of the First Non-Na Value in an R Vector
Dependency 'Slam' Is Not Available When Installing Tm Package
How to Generate Ascii "Graphical Output" from R
Join Data.Table on Exact Date or If Not the Case on the Nearest Less Than Date
How to Extract Substring Between Patterns "_" and "." in R
Change Text Color for Cells Using Tablegrob
Replacing All Occurrences of a Pattern in a String
How to Find Index of Match Between Two Set of Data Frame
How to Create, Structure, Maintain and Update Data Codebooks in R
Listing R Package Dependencies Without Installing Packages
Alternatives to Nested Ifelse Statements in R
Setting the Color for an Individual Data Point