1

comp/INFO_MAP_ECE/101102.1.119

This string is the output of a CPU but there are always special/non-printable characters after the number and my aim is to obtain the number excluding the text before it and special/non-printable after it. I am trying the split method but am not sure what to use for special/non-printable characters. Can anyone please suggest something? It would be a great help. thanks.

2
  • 1
    I suggest you show us the code that you use to extract the text Commented Sep 7, 2011 at 13:05
  • If one of the answers you got solved your problem, please accept it by clicking on the check mark. Commented Sep 27, 2011 at 3:10

2 Answers 2

3

Assuming your output always looks something like what you showed, you can use a regular expression:

numPattern = r'/([\d.]+)'
output = 'comp/INFO_MAP_ECE/101102.1.119'

m = re.search(numPattern, output)

if m: #If a match was found
  numString = m.group(1)  #Extracts the first group surrounded by ()
  #etc

The pattern here looks for a /, then some numbers and periods, then anything, and extracts just the numbers and periods. This should work as long as you're always getting a string that matches that description.

HTH!

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

3 Comments

As I know . doesn't have to be escaped in a character class. So, instead of [\d\.]+', the pattern should be [\d.]+'.
Also, I don't understand, what .* is doing at the end of the pattern.
Changed. I didn't know about .s in character classes, so that's good to know. I'm not entirely sure what the .* was doing there either. Maybe I was thinking about the non-printable characters?
1

Is the number always the same length? If so you could just slice the string.

'comp/INFO_MAP_ECE/101102.1.119'[18:30]

1 Comment

Not a good idea. Even if the number is the same length now, it may not always be. Also becomes pretty onerous to maintain. This is precisely what regexes were made for.

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.