1

In this example I need salt value in both strings.

string = 'SHA-224(Salt len: 20)'

string2 = '# Salt len: 0' 
 
pattern = re.compile(r'^(#\s)?((SHA-\d+)?\(?Salt len: \d+\)?)') 
 
result = pattern.search(string)  

result2 = pattern.search(string2)

print(result)

print(result2)   

Expected output:
20
0

please help me guys

1
  • 1
    Is this working for you? Commented Jun 22, 2020 at 9:33

1 Answer 1

1

You may use a simpler pattern that will match Salt len:, spaces and then captures any amount of digits:

pattern = re.compile(r'\bSalt len: (\d+)')

See the regex demo.

Details

  • \b - a word boundary
  • Salt len: - a string
  • (\d+) - Capturing group 1: one or more digits

When using in Python, check for a match first:

result = pattern.search(string)

Then, check if there was a match, and if yes, extract Group 1 value.

Python demo:

import re
texts = ['SHA-224(Salt len: 20)', '# Salt len: 0']
pattern = re.compile(r'\bSalt len: (\d+)') 
for text in texts:
    result = pattern.search(text)
    if result:
        print(result.group(1))
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.