Implement `String#to_cents`, which should parse prices expressed as `$1.23` and return number of cents, or in case of bad format return `nil`.

Difficulty: introductory

Starter code:

def to_cents(amount):
	

Solutions:
import re


def to_cents(amount):
    m = re.match(r'\$(\d+)\.(\d\d)\Z', amount)
    return int(m.expand(r'\1\2')) if m else None


from re import compile, match

REGEX = compile(r'\$(?P<integer>\d+)\.(?P<frac>\d{0,2})$')


def to_cents(amount):
    m = match(REGEX, amount)
    if m and m.group(0) == amount:
        return int(m.group('integer')) * 100 + int(m.group('frac'))


def to_cents(amount):
    if amount and amount[0] == '$':
        try:
            return int(amount[1:-3] + amount[-2:])
        except:
            return None
    return None

import re

def to_cents(amount):
    matched = re.search(r'\A\$(\d+).(\d{2})\Z', amount)
    if matched:
        return int('{}{}'.format(*matched.groups()))

from re import match

def to_cents(amount):
    res = match('^\$(\d*)\.(\d{2})(?!\n)$', amount)
    return int(''.join(res.groups())) if res else None

import re
def to_cents(amount):
    price = re.match(r'^\$(\d+)\.(\d{1,2})\Z', amount)
    return int(price.expand(r'\1\2')) if price else None

import re
def to_cents(amount):
    return int(re.sub('\$|\.', '', amount)) if re.search(r'^\$\d+\.\d{1,2}\Z', amount) else None

import re
def to_cents(amount):
  return int(re.sub("\D", "", amount)) if re.match("\$\d*\.\d{2}\Z", amount) else None

import re

def to_cents(amount):

    if not re.match(r"^\$(0|[1-9]+\d*)\.\d{2}\Z", amount):
      return None
      
    return int(re.sub(r"[\$\.]", "", amount))

import re
def to_cents(amount):
    pattern = re.compile(r'^\$([\d]+)\.([\d]{2})$')
    if re.fullmatch(pattern, amount) == None:
        return None
    price = re.fullmatch(pattern, amount)
    return (int(price.group(1)) * 100) + int(price.group(2))