Implement a function/class, which should return an integer if the input string is in one of the formats specified below, or `null/nil/None` otherwise.

Format:
* Optional `-` or `+`
* Base prefix `0b` (binary), `0x` (hexadecimal), `0o` (octal), or in case of no prefix decimal.
* Digits depending on base

Any extra character (including whitespace) makes the input invalid, in which case you should return `null/nil/None`.

Digits are case insensitive, but base prefix must be lower case.

See the test cases for examples.

Difficulty: introductory

Starter code:

def to_integer(string):
	

Solutions:
import re

def to_integer(s):
    if re.match("\A[+-]?(\d+|0b[01]+|0o[0-7]+|0x[0-9a-fA-F]+)\Z", s):
        return int(s, 10 if s[1:].isdigit() else 0)

from re import compile, match

REGEX = compile(r'[+-]?(0(?P<base>[bxo]))?[\d\w]+\Z')


def to_integer(strng):
    try:
        return int(strng, 0 if match(REGEX, strng).group('base') else 10)
    except (AttributeError, KeyError, ValueError):
        pass


import re
def to_integer(s):
    decimal = re.compile(r'^[-+]?\d+$')
    binary = re.compile(r'^[-+]?0b[01]+$')
    hexadecimal = re.compile(r'^[-+]?0x[0-9A-Fa-f]+$')
    octal = re.compile(r'^[-+]?0o[0-7]+$')
    return next((int(s,[10,0][j!=0]) for j,i in enumerate([decimal,binary,hexadecimal,octal]) if re.search(i,s) and '\n' not in s),None)

import re

BASE = {'0b': 2, '0x': 16, '0o': 8, '': 10}
def to_integer(string):
    matched = re.search(r'\A(?P<sign>[-+]?)(?P<base>0[box]|)?(?P<n>[a-fA-F0-9]+)\Z', string)
    if not matched:
        return None
    base = BASE[matched.group('base').lower()]
    try:
        n = int(matched.group('n'), base)
    except ValueError:
        return None
    return n * (-1 if matched.group('sign') == '-' else 1)

import re

def to_integer(string):
    valid = re.search(r"\A([+-])?(0[bxo])?([0-9a-fA-F]+)\Z", string)
    if not valid:
        return None
    str_base = valid.groups()[1]
    base = {"0x": 16, "0o": 8, "0b": 2}.get(str_base, 10)
    try:
        return int(string, base)
    except:
        return None


import re


def to_integer(string):
    #your code here
    result = re.compile(r'^(\+|-)?(0b[01]+|0o[0-7]+|0x(\d|[abcdefABCDEF])+|\d+)\Z').match(string)
    if not result:
        return None     
    return convert_to_number(result.string)
        

def convert_to_number(string):
    base_dict={'0x': 16, '0b': 2, '0o':8}
    for key, value in base_dict.items():
        if string.find(key) != -1:
            return int(string, value)
    return int(string)

import re

def to_integer(string):
    try: return eval(''.join(e for e in re.match(r'\A([-+]?)(?:(0b[01]+)|(0o[0-7]+)|(0x[0-9a-fA-F]+)|0*(\d+))\Z',string).groups() if e))
    except: return None

import re
def to_integer(string):
    s = re.match("^([+-]?((0x[0-9A-Fa-f]+)|(0b[01]+)|(0o[0-7]+))?(?(2)$|[0-9]+$))",string)
    if s==None or s.group(1)!=string:
        return None
    return int(string,0 if s.group(2) else 10)

import re
def to_integer(string):
    b = r'\A[+-]?0b1+[01]*\Z'
    x = r'\A[+-]?0x[1-9A-Fa-f]+[0-9A-Fa-f]*\Z'
    o = r'\A[+-]?0o[1-7]+[0-7]*\Z'
    d = r'\A[+-]?[0-9]+\Z'
    #print(string)
    for pattern, base in zip((b,x,o,d), (2,16,8,10)):
        if re.search(pattern, string):
            return int(string, base)
    return None

import re

def to_integer(string):
    dic = {'0x': 16, '0b': 2, '0o': 8 ,None: 10}
    match = re.fullmatch(r"[+-]?(0[xbo]+)?[\d\w]*", string)
    try:
        num = int(match.group(), dic[match.group(1)])
    except:
        num = None
    finally:
        return num