0

I have a string

import re
name = 'simranjeet kumar'

print (re.findall(r'^s.', name))

output: ['si']

But I am expecting ['simranjeet'],may I know why I am not getting simranjeet and why I am getting only ['si'] I mean string of length 2.

1
  • note, that findall is for matching all(multiple) occurrences of substring. Commented Nov 21, 2016 at 7:34

1 Answer 1

1

In regular expressions . means any ONE symbol. To extract MANY any symbols use + or *. You are extracting a word until the space. I would solve this task like this:

re.findall(r'^(.+?)\s', name)
# or
re.findall(r'^(s.+?)\s', name)
# or
re.findall(r'^(\S+)', name)
# or
re.findall(r'^(s\S+)', name)

\s means any space symbol. \S means any non-space symbol. See wikipedia for more information.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.