Output a string starting with a specific character from the string

Asked 2 years ago, Updated 2 years ago, 50 views

import re


state_names=['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','NewJersey','NewMexico','NewYork','NorthCarolina','NorthDakota','Ohio','Oklahoma','Oregon','Pennsylvania','RhodeIsland','SouthCarolina','SouthDakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','WestVirginia','Wisconsin','Wyoming']

a = str(state_names)
b=re.findall('^M', a)

print(b)

I want to replace state_names with a string and print out all the characters that start with M, but if I write it like this, only [] is output. Did something go wrong?

regex

2022-09-20 10:22

1 Answers

b=re.findall('\'M\w+\'', a)

Switch to and try. I think you can study more about regular expressions.


2022-09-20 10:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.