1

I have a string

81.5r3

I need to create a regex pattern, So that I should get a exact list as mentioned below

['81.5', 'r', '3']

The pattern should be float, char, int

Is there any way to achieve this?

7
  • You need to be more specific about the patterns of data you want to match. Otherwise this question is too broad. As currently written, someone could answer with (81\.5)(r)(3) Commented Jan 7, 2021 at 4:07
  • @Nick I have mentioned how I need the output. What else should I mention? Commented Jan 7, 2021 at 4:11
  • float, char and int? Commented Jan 7, 2021 at 4:12
  • "the patterns of data you want to match". Are they all a floating point number, a letter, and a digit? Or is the pattern different? Commented Jan 7, 2021 at 4:12
  • @python_user Yes Commented Jan 7, 2021 at 4:15

3 Answers 3

2

Try something like regex101.com for quick experimentation?

For a "float, char, int" sequence, it'll be something like:

(-?[0-9]+(?:\.[0-9]+)?)(.)(-?[0-9]+)

You can refine it if you want to do things like forbid leading/trailing zeros:

(-?[1-9][0-9]*(?:\.[0-9]*[1-9])?)(.)(-?[1-9][0-9]*)

You may also need to refine it if you need to support exponential notation (1.2e3) or NaN/Inf values for the float part.

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

Comments

2
(\d+[.]\d)([a-z])(\d)

I made a simple regex. I think that's enough for you to upgrade it, good luck, and happy new year.

Please check

https://regex101.com/r/kFYzag/5

Please use regex101 to try your idea.

Image result Result

Comments

1
 re.split(r"([a-z])", '81.5r3')

You can use re.split using a capture group to get what you want.

The output would be ['81.5', 'r', '3']

1 Comment

You can mark this as accepted if it helped you

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.