Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
No Match

/
/
gim

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"(\:(\w|\+|\-)+\:)(?=\s|[\!\.\?]|$)" test_str = (":+1:\n" ":1234:\n" ":100:\n" ":1234:\n" ":+1:\n" ":-1:\n" ":8ball:\n" ":a:\n" ":ab:\n" ":abc:\n" ":abcd:\n" ":accept:\n" ":aerial_tramway:\n" ":airplane:\n" ":alarm_clock:\n" ":alien:\n" ":ambulance:\n" ":anchor:\n" ":angel:\n" ":anger:\n" ":angry:\n" ":anguished:\n" ":ant:\n" ":apple:\n" ":aquarius:\n" ":aries:\n" ":arrow_backward:\n" ":arrow_double_down:\n" ":arrow_double_up:\n" ":arrow_down:\n" ":arrow_down_small:\n" ":arrow_forward:\n" ":arrow_heading_down:\n" ":arrow_heading_up:\n" ":arrow_left:\n" ":arrow_lower_left:\n" ":arrow_lower_right:\n" ":arrow_right:\n" ":arrow_right_hook:\n" ":arrow_up:\n" ":arrow_up_down:\n" ":arrow_up_small:\n" ":arrow_upper_left:\n" ":arrow_upper_right:\n" ":arrows_clockwise:\n" ":arrows_counterclockwise:\n" ":art:\n" ":articulated_lorry:\n" ":astonished:\n" ":athletic_shoe:\n" ":atm:\n" ":b:\n" ":baby:\n" ":baby_bottle:\n" ":baby_chick:\n" ":baby_symbol:\n" ":back:\n" ":baggage_claim:\n" ":balloon:\n" ":ballot_box_with_check:\n" ":bamboo:\n" ":banana:\n" ":bangbang:\n" ":bank:\n" ":bar_chart:\n" ":barber:\n" ":baseball:\n" ":basketball:\n" ":bath:\n" ":bathtub:\n" ":battery:\n" ":bear:\n" ":bee:\n" ":beer:\n" ":beers:\n" ":beetle:\n" ":beginner:\n" ":bell:\n" ":bento:\n" ":bicyclist:\n" ":bike:\n" ":bikini:\n" ":bird:\n" ":birthday:\n" ":black_circle:\n" ":black_joker:\n" ":black_large_square:\n" ":black_medium_small_square:\n" ":black_medium_square:\n" ":black_nib:\n" ":black_small_square:\n" ":black_square_button:\n" ":blossom:\n" ":blowfish:\n" ":blue_book:\n" ":blue_car:\n" ":blue_heart:\n" ":blush:\n" ":boar:\n" ":boat:\n" ":bomb:\n" ":book:\n" ":bookmark:\n" ":bookmark_tabs:\n" ":books:\n" ":boom:\n" ":boot:\n" ":bouquet:\n" ":bow:\n" ":bowling:\n" ":bowtie:\n" ":boy:\n" ":bread:\n" ":bride_with_veil:\n" ":bridge_at_night:\n" ":briefcase:\n" ":broken_heart:\n" ":bug:\n" ":bulb:\n" ":bullettrain_front:\n" ":bullettrain_side:\n" ":bus:\n" ":busstop:\n" ":bust_in_silhouette:\n" ":busts_in_silhouette:\n" ":cactus:\n" ":cake:\n" ":calendar:\n" ":calling:\n" ":camel:\n" ":camera:\n" ":cancer:\n" ":candy:\n" ":capital_abcd:\n" ":capricorn:\n" ":car:\n" ":card_index:\n" ":carousel_horse:\n" ":cat:\n" ":cat2:\n" ":cd:\n" ":chart:\n" ":chart_with_downwards_trend:\n" ":chart_with_upwards_trend:\n" ":checkered_flag:\n" ":cherries:\n" ":cherry_blossom:\n" ":chestnut:\n" ":chicken:\n" ":children_crossing:\n" ":chocolate_bar:\n" ":christmas_tree:\n" ":church:\n" ":cinema:\n" ":circus_tent:\n" ":city_sunrise:\n" ":city_sunset:\n" ":cl:\n" ":clap:\n" ":clapper:\n" ":clipboard:\n" ":clock1:\n" ":clock10:\n" ":clock1030:\n" ":clock11:\n" ":clock1130:\n" ":clock12:\n" ":clock1230:\n" ":clock130:\n" ":clock2:\n" ":clock230:\n" ":clock3:\n" ":clock330:\n" ":clock4:\n" ":clock430:\n" ":clock5:\n" ":clock530:\n" ":clock6:\n" ":clock630:\n" ":clock7:\n" ":clock730:\n" ":clock8:\n" ":clock830:\n" ":clock9:\n" ":clock930:\n" ":closed_book:\n" ":closed_lock_with_key:\n" ":closed_umbrella:\n" ":cloud:\n" ":clubs:\n" ":cn:\n" ":cocktail:\n" ":coffee:\n" ":cold_sweat:\n" ":collision:\n" ":computer:\n" ":confetti_ball:\n" ":confounded:\n" ":confused:\n" ":congratulations:\n" ":construction:\n" ":construction_worker:\n" ":convenience_store:\n" ":cookie:\n" ":cool:\n" ":cop:\n" ":copyright:\n" ":corn:\n" ":couple:\n" ":couple_with_heart:\n" ":couplekiss:\n" ":cow:\n" ":cow2:\n" ":credit_card:\n" ":crescent_moon:\n" ":crocodile:\n" ":crossed_flags:\n" ":crown:\n" ":cry:\n" ":crying_cat_face:\n" ":crystal_ball:\n" ":cupid:\n" ":curly_loop:\n" ":currency_exchange:\n" ":curry:\n" ":custard:\n" ":customs:\n" ":cyclone:\n" ":dancer:\n" ":dancers:\n" ":dango:\n" ":dart:\n" ":dash:\n" ":date:\n" ":de:\n" ":deciduous_tree:\n" ":department_store:\n" ":diamond_shape_with_a_dot_inside:\n" ":diamonds:\n" ":disappointed:\n" ":disappointed_relieved:\n" ":dizzy:\n" ":dizzy_face:\n" ":do_not_litter:\n" ":dog:\n" ":dog2:\n" ":dollar:\n" ":dolls:\n" ":dolphin:\n" ":door:\n" ":doughnut:\n" ":dragon:\n" ":dragon_face:\n" ":dress:\n" ":dromedary_camel:\n" ":droplet:\n" ":dvd:\n" ":e-mail:\n" ":ear:\n" ":ear_of_rice:\n" ":earth_africa:\n" ":earth_americas:\n" ":earth_asia:\n" ":egg:\n" ":eggplant:\n" ":eight:\n" ":eight_pointed_black_star:\n" ":eight_spoked_asterisk:\n" ":electric_plug:\n" ":elephant:\n" ":email:\n" ":end:\n" ":envelope:\n" ":envelope_with_arrow:\n" ":es:\n" ":euro:\n" ":european_castle:\n" ":european_post_office:\n" ":evergreen_tree:\n" ":exclamation:\n" ":expressionless:\n" ":eyeglasses:\n" ":eyes:\n" ":facepunch:\n" ":factory:\n" ":fallen_leaf:\n" ":family:\n" ":fast_forward:\n" ":fax:\n" ":fearful:\n" ":feelsgood:\n" ":feet:\n" ":ferris_wheel:\n" ":file_folder:\n" ":finnadie:\n" ":fire:\n" ":fire_engine:\n" ":fireworks:\n" ":first_quarter_moon:\n" ":first_quarter_moon_with_face:\n" ":fish:\n" ":fish_cake:\n" ":fishing_pole_and_fish:\n" ":fist:\n" ":five:\n" ":flags:\n" ":flashlight:\n" ":flipper:\n" ":floppy_disk:\n" ":flower_playing_cards:\n" ":flushed:\n" ":foggy:\n" ":football:\n" ":footprints:\n" ":fork_and_knife:\n" ":fountain:\n" ":four:\n" ":four_leaf_clover:\n" ":fr:\n" ":free:\n" ":fried_shrimp:\n" ":fries:\n" ":frog:\n" ":frowning:\n" ":fu:\n" ":fuelpump:\n" ":full_moon:\n" ":full_moon_with_face:\n" ":game_die:\n" ":gb:\n" ":gem:\n" ":gemini:\n" ":ghost:\n" ":gift:\n" ":gift_heart:\n" ":girl:\n" ":globe_with_meridians:\n" ":goat:\n" ":goberserk:\n" ":godmode:\n" ":golf:\n" ":grapes:\n" ":green_apple:\n" ":green_book:\n" ":green_heart:\n" ":grey_exclamation:\n" ":grey_question:\n" ":grimacing:\n" ":grin:\n" ":grinning:\n" ":guardsman:\n" ":guitar:\n" ":gun:\n" ":haircut:\n" ":hamburger:\n" ":hammer:\n" ":hamster:\n" ":hand:\n" ":handbag:\n" ":hankey:\n" ":hash:\n" ":hatched_chick:\n" ":hatching_chick:\n" ":headphones:\n" ":hear_no_evil:\n" ":heart:\n" ":heart_decoration:\n" ":heart_eyes:\n" ":heart_eyes_cat:\n" ":heartbeat:\n" ":heartpulse:\n" ":hearts:\n" ":heavy_check_mark:\n" ":heavy_division_sign:\n" ":heavy_dollar_sign:\n" ":heavy_exclamation_mark:\n" ":heavy_minus_sign:\n" ":heavy_multiplication_x:\n" ":heavy_plus_sign:\n" ":helicopter:\n" ":herb:\n" ":hibiscus:\n" ":high_brightness:\n" ":high_heel:\n" ":hocho:\n" ":honey_pot:\n" ":honeybee:\n" ":horse:\n" ":horse_racing:\n" ":hospital:\n" ":hotel:\n" ":hotsprings:\n" ":hourglass:\n" ":hourglass_flowing_sand:\n" ":house:\n" ":house_with_garden:\n" ":hurtrealbad:\n" ":hushed:\n" ":ice_cream:\n" ":icecream:\n" ":id:\n" ":ideograph_advantage:\n" ":imp:\n" ":inbox_tray:\n" ":incoming_envelope:\n" ":information_desk_person:\n" ":information_source:\n" ":innocent:\n" ":interrobang:\n" ":iphone:\n" ":it:\n" ":izakaya_lantern:\n" ":jack_o_lantern:\n" ":japan:\n" ":japanese_castle:\n" ":japanese_goblin:\n" ":japanese_ogre:\n" ":jeans:\n" ":joy:\n" ":joy_cat:\n" ":jp:\n" ":key:\n" ":keycap_ten:\n" ":kimono:\n" ":kiss:\n" ":kissing:\n" ":kissing_cat:\n" ":kissing_closed_eyes:\n" ":kissing_heart:\n" ":kissing_smiling_eyes:\n" ":knife:\n" ":koala:\n" ":koko:\n" ":kr:\n" ":lantern:\n" ":large_blue_circle:\n" ":large_blue_diamond:\n" ":large_orange_diamond:\n" ":last_quarter_moon:\n" ":last_quarter_moon_with_face:\n" ":laughing:\n" ":leaves:\n" ":ledger:\n" ":left_luggage:\n" ":left_right_arrow:\n" ":leftwards_arrow_with_hook:\n" ":lemon:\n" ":leo:\n" ":leopard:\n" ":libra:\n" ":light_rail:\n" ":link:\n" ":lips:\n" ":lipstick:\n" ":lock:\n" ":lock_with_ink_pen:\n" ":lollipop:\n" ":loop:\n" ":loud_sound:\n" ":loudspeaker:\n" ":love_hotel:\n" ":love_letter:\n" ":low_brightness:\n" ":m:\n" ":mag:\n" ":mag_right:\n" ":mahjong:\n" ":mailbox:\n" ":mailbox_closed:\n" ":mailbox_with_mail:\n" ":mailbox_with_no_mail:\n" ":man:\n" ":man_with_gua_pi_mao:\n" ":man_with_turban:\n" ":mans_shoe:\n" ":maple_leaf:\n" ":mask:\n" ":massage:\n" ":meat_on_bone:\n" ":mega:\n" ":melon:\n" ":memo:\n" ":mens:\n" ":metal:\n" ":metro:\n" ":microphone:\n" ":microscope:\n" ":milky_way:\n" ":minibus:\n" ":minidisc:\n" ":mobile_phone_off:\n" ":money_with_wings:\n" ":moneybag:\n" ":monkey:\n" ":monkey_face:\n" ":monorail:\n" ":moon:\n" ":mortar_board:\n" ":mount_fuji:\n" ":mountain_bicyclist:\n" ":mountain_cableway:\n" ":mountain_railway:\n" ":mouse:\n" ":mouse2:\n" ":movie_camera:\n" ":moyai:\n" ":muscle:\n" ":mushroom:\n" ":musical_keyboard:\n" ":musical_note:\n" ":musical_score:\n" ":mute:\n" ":nail_care:\n" ":name_badge:\n" ":neckbeard:\n" ":necktie:\n" ":negative_squared_cross_mark:\n" ":neutral_face:\n" ":new:\n" ":new_moon:\n" ":new_moon_with_face:\n" ":newspaper:\n" ":ng:\n" ":night_with_stars:\n" ":nine:\n" ":no_bell:\n" ":no_bicycles:\n" ":no_entry:\n" ":no_entry_sign:\n" ":no_good:\n" ":no_mobile_phones:\n" ":no_mouth:\n" ":no_pedestrians:\n" ":no_smoking:\n" ":non-potable_water:\n" ":nose:\n" ":notebook:\n" ":notebook_with_decorative_cover:\n" ":notes:\n" ":nut_and_bolt:\n" ":o:\n" ":o2:\n" ":ocean:\n" ":octocat:\n" ":octopus:\n" ":oden:\n" ":office:\n" ":ok:\n" ":ok_hand:\n" ":ok_woman:\n" ":older_man:\n" ":older_woman:\n" ":on:\n" ":oncoming_automobile:\n" ":oncoming_bus:\n" ":oncoming_police_car:\n" ":oncoming_taxi:\n" ":one:\n" ":open_book:\n" ":open_file_folder:\n" ":open_hands:\n" ":open_mouth:\n" ":ophiuchus:\n" ":orange_book:\n" ":outbox_tray:\n" ":ox:\n" ":package:\n" ":page_facing_up:\n" ":page_with_curl:\n" ":pager:\n" ":palm_tree:\n" ":panda_face:\n" ":paperclip:\n" ":parking:\n" ":part_alternation_mark:\n" ":partly_sunny:\n" ":passport_control:\n" ":paw_prints:\n" ":peach:\n" ":pear:\n" ":pencil:\n" ":pencil2:\n" ":penguin:\n" ":pensive:\n" ":performing_arts:\n" ":persevere:\n" ":person_frowning:\n" ":person_with_blond_hair:\n" ":person_with_pouting_face:\n" ":phone:\n" ":pig:\n" ":pig2:\n" ":pig_nose:\n" ":pill:\n" ":pineapple:\n" ":pisces:\n" ":pizza:\n" ":point_down:\n" ":point_left:\n" ":point_right:\n" ":point_up:\n" ":point_up_2:\n" ":police_car:\n" ":poodle:\n" ":poop:\n" ":post_office:\n" ":postal_horn:\n" ":postbox:\n" ":potable_water:\n" ":pouch:\n" ":poultry_leg:\n" ":pound:\n" ":pouting_cat:\n" ":pray:\n" ":princess:\n" ":punch:\n" ":purple_heart:\n" ":purse:\n" ":pushpin:\n" ":put_litter_in_its_place:\n" ":question:\n" ":rabbit:\n" ":rabbit2:\n" ":racehorse:\n" ":radio:\n" ":radio_button:\n" ":rage:\n" ":rage1:\n" ":rage2:\n" ":rage3:\n" ":rage4:\n" ":railway_car:\n" ":rainbow:\n" ":raised_hand:\n" ":raised_hands:\n" ":raising_hand:\n" ":ram:\n" ":ramen:\n" ":rat:\n" ":recycle:\n" ":red_car:\n" ":red_circle:\n" ":registered:\n" ":relaxed:\n" ":relieved:\n" ":repeat:\n" ":repeat_one:\n" ":restroom:\n" ":revolving_hearts:\n" ":rewind:\n" ":ribbon:\n" ":rice:\n" ":rice_ball:\n" ":rice_cracker:\n" ":rice_scene:\n" ":ring:\n" ":rocket:\n" ":roller_coaster:\n" ":rooster:\n" ":rose:\n" ":rotating_light:\n" ":round_pushpin:\n" ":rowboat:\n" ":ru:\n" ":rugby_football:\n" ":runner:\n" ":running:\n" ":running_shirt_with_sash:\n" ":sa:\n" ":sagittarius:\n" ":sailboat:\n" ":sake:\n" ":sandal:\n" ":santa:\n" ":satellite:\n" ":satisfied:\n" ":saxophone:\n" ":school:\n" ":school_satchel:\n" ":scissors:\n" ":scorpius:\n" ":scream:\n" ":scream_cat:\n" ":scroll:\n" ":seat:\n" ":secret:\n" ":see_no_evil:\n" ":seedling:\n" ":seven:\n" ":shaved_ice:\n" ":sheep:\n" ":shell:\n" ":ship:\n" ":shipit:\n" ":shirt:\n" ":shit:\n" ":shoe:\n" ":shower:\n" ":signal_strength:\n" ":six:\n" ":six_pointed_star:\n" ":ski:\n" ":skull:\n" ":sleeping:\n" ":sleepy:\n" ":slot_machine:\n" ":small_blue_diamond:\n" ":small_orange_diamond:\n" ":small_red_triangle:\n" ":small_red_triangle_down:\n" ":smile:\n" ":smile_cat:\n" ":smiley:\n" ":smiley_cat:\n" ":smiling_imp:\n" ":smirk:\n" ":smirk_cat:\n" ":smoking:\n" ":snail:\n" ":snake:\n" ":snowboarder:\n" ":snowflake:\n" ":snowman:\n" ":sob:\n" ":soccer:\n" ":soon:\n" ":sos:\n" ":sound:\n" ":space_invader:\n" ":spades:\n" ":spaghetti:\n" ":sparkle:\n" ":sparkler:\n" ":sparkles:\n" ":sparkling_heart:\n" ":speak_no_evil:\n" ":speaker:\n" ":speech_balloon:\n" ":speedboat:\n" ":squirrel:\n" ":star:\n" ":star2:\n" ":stars:\n" ":station:\n" ":statue_of_liberty:\n" ":steam_locomotive:\n" ":stew:\n" ":straight_ruler:\n" ":strawberry:\n" ":stuck_out_tongue:\n" ":stuck_out_tongue_closed_eyes:\n" ":stuck_out_tongue_winking_eye:\n" ":sun_with_face:\n" ":sunflower:\n" ":sunglasses:\n" ":sunny:\n" ":sunrise:\n" ":sunrise_over_mountains:\n" ":surfer:\n" ":sushi:\n" ":suspect:\n" ":suspension_railway:\n" ":sweat:\n" ":sweat_drops:\n" ":sweat_smile:\n" ":sweet_potato:\n" ":swimmer:\n" ":symbols:\n" ":syringe:\n" ":tada:\n" ":tanabata_tree:\n" ":tangerine:\n" ":taurus:\n" ":taxi:\n" ":tea:\n" ":telephone:\n" ":telephone_receiver:\n" ":telescope:\n" ":tennis:\n" ":tent:\n" ":thought_balloon:\n" ":three:\n" ":thumbsdown:\n" ":thumbsup:\n" ":ticket:\n" ":tiger:\n" ":tiger2:\n" ":tired_face:\n" ":tm:\n" ":toilet:\n" ":tokyo_tower:\n" ":tomato:\n" ":tongue:\n" ":top:\n" ":tophat:\n" ":tractor:\n" ":traffic_light:\n" ":train:\n" ":train2:\n" ":tram:\n" ":triangular_flag_on_post:\n" ":triangular_ruler:\n" ":trident:\n" ":triumph:\n" ":trolleybus:\n" ":trollface:\n" ":trophy:\n" ":tropical_drink:\n" ":tropical_fish:\n" ":truck:\n" ":trumpet:\n" ":tshirt:\n" ":tulip:\n" ":turtle:\n" ":tv:\n" ":twisted_rightwards_arrows:\n" ":two:\n" ":two_hearts:\n" ":two_men_holding_hands:\n" ":two_women_holding_hands:\n" ":u5272:\n" ":u5408:\n" ":u55b6:\n" ":u6307:\n" ":u6708:\n" ":u6709:\n" ":u6e80:\n" ":u7121:\n" ":u7533:\n" ":u7981:\n" ":u7a7a:\n" ":uk:\n" ":umbrella:\n" ":unamused:\n" ":underage:\n" ":unlock:\n" ":up:\n" ":us:\n" ":v:\n" ":vertical_traffic_light:\n" ":vhs:\n" ":vibration_mode:\n" ":video_camera:\n" ":video_game:\n" ":violin:\n" ":virgo:\n" ":volcano:\n" ":vs:\n" ":walking:\n" ":waning_crescent_moon:\n" ":waning_gibbous_moon:\n" ":warning:\n" ":watch:\n" ":water_buffalo:\n" ":watermelon:\n" ":wave:\n" ":wavy_dash:\n" ":waxing_crescent_moon:\n" ":waxing_gibbous_moon:\n" ":wc:\n" ":weary:\n" ":wedding:\n" ":whale:\n" ":whale2:\n" ":wheelchair:\n" ":white_check_mark:\n" ":white_circle:\n" ":white_flower:\n" ":white_large_square:\n" ":white_medium_small_square:\n" ":white_medium_square:\n" ":white_small_square:\n" ":white_square_button:\n" ":wind_chime:\n" ":wine_glass:\n" ":wink:\n" ":wolf:\n" ":woman:\n" ":womans_clothes:\n" ":womans_hat:\n" ":womens:\n" ":worried:\n" ":wrench:\n" ":x:\n" ":yellow_heart:\n" ":yen:\n" ":yum:\n" ":zap:\n" ":zero:\n" ":zzz:") matches = re.finditer(regex, test_str, re.IGNORECASE | re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html