Regex: Match a Word With Exactly One Occurrence of a Specified Character At the Beginning

RegEx: Match a word with exactly one occurrence of a specified character at the beginning

The regex you tried [^\$]\$\w+ will match not a dollar sign followed by a dollar sign and one or more times a word character. That would match for example a$Cheatsheet or $Cheatsheet with a leading space. Note that you don't have to escape the dollar sign in the character class.

If negative lookbehinds are supported, to match a word that does not start with a dollar sign you could use:


Regex demo

Without a lookbehind you could match what you don't want and capture what you do want in a capturing group.


Regex demo

If the dollar sign can also not be in the middle of the word you could use:


Regex demo

Regex match any single character (one character only)

Match any single character

  • Use the dot . character as a wildcard to match any single character.

Example regex: a.c

abc   // match
a c // match
azc // match
ac // no match
abbc // no match

Match any specific character in a set

  • Use square brackets [] to match any characters in a set.
  • Use \w to match any single alphanumeric character: 0-9, a-z, A-Z, and _ (underscore).
  • Use \d to match any single digit.
  • Use \s to match any single whitespace character.

Example 1 regex: a[bcd]c

abc   // match
acc // match
adc // match
ac // no match
abbc // no match

Example 2 regex: a[0-7]c

a0c   // match
a3c // match
a7c // match
a8c // no match
ac // no match
a55c // no match

Match any character except ...

Use the hat in square brackets [^] to match any single character except for any of the characters that come after the hat ^.

Example regex: a[^abc]c

aac   // no match
abc // no match
acc // no match
a c // match
azc // match
ac // no match
azzc // no match

(Don't confuse the ^ here in [^] with its other usage as the start of line character: ^ = line start, $ = line end.)

Match any character optionally

Use the optional character ? after any character to specify zero or one occurrence of that character. Thus, you would use .? to match any single character optionally.

Example regex: a.?c

abc   // match
a c // match
azc // match
ac // match
abbc // no match

See also

  • A quick tutorial to teach you the basics of regex
  • A practice sandbox to try things out

RegEx to match exactly one occurrence of character at the end of string in Javascript

If "m" and "s" must be preceded by at least one or more numbers \d+ use:


Demo on Regex101

1m      // matches
123m // matches
1s // matches
123s // matches

PS: if you already use [ms] (meaning "m or s") then the {1} is unnecessary since it matches only one option.

Regex: matching up to the first occurrence of a character

You need


The [^;] is a character class, it matches everything but a semicolon.

^ (start of line anchor) is added to the beginning of the regex so only the first match on each line is captured. This may or may not be required, depending on whether possible subsequent matches are desired.

To cite the perlre manpage:

You can specify a character class, by enclosing a list of characters in [] , which will match any character from the list. If the first character after the "[" is "^", the class matches any character not in the list.

This should work in most regex dialects.

Regex, only match with strings that contain one occurence of certain characters

Since you only want to match arithmetic expressions and integers I changed your code to find everything if it has numbers, operators or a equal sign: ^[-+*\/\d=]+$. I needed to escape the character / -> \/

To match only one equal sign I addes a negative lookahead: (?!.*=.*=). If it finds whats insinde the brackets, the whole regex wont match. For example if you have a word mytext : (?!mytext) the whole regex won't match. .* means it finds everything (. stands for every character and the * say's it can be there 0 to unlimited times).

So this is the solution:


See live example here:

Also your code didn't worked for -4*-4.

Edit: Since you don't want your code to start with / or * I added id in the negative lookahead: ^(?!.*=.*=|[\/*].*)[-+*\/\d=]+$

Cannot match exactly one occurence using regex

For exact matching, you need to use anchors. And also it must be [0-9] not {0-9}, note the brackets used.

# [1] "1"
# [1] "1223"

last one returns nothing because there isn't a number which exactly contain three digits.

Regex to check if it is exactly one single word

To convert your pattern to a regexp, you first need to make sure each character is interpreted literally and not as a special character. We can do that by inserting a \ in front of any re special character. Those characters can be obtained through sre_parse.SPECIAL_CHARS.

Since you have a special meaning for *, we do not want to escape that one but instead replace it by \w+.


import sre_parse

def convert_to_regexp(pattern):
special_characters = set(sre_parse.SPECIAL_CHARS)

safe_pattern = ''.join(['\\' + c if c in special_characters else c for c in pattern ])

return safe_pattern.replace('*', '\\w+')

import re

pattern = '*.key'
r_pattern = convert_to_regexp(pattern) # '\\w+\\.key'

re.match(r_pattern, 'door.key') # Match
re.match(r_pattern, 'brown.door.key') # None

And here is an example with escaped special characters

pattern = '*.(key)'
r_pattern = convert_to_regexp(pattern) # '\\w+\\.\\(key\\)'

re.match(r_pattern, 'door.(key)') # Match
re.match(r_pattern, 'brown.door.(key)') # None

If you intend looking for the output pattern with or re.findall, you might want to wrap the re pattern between \b boundary characters.

Related Topics

Leave a reply