0

I have a string

1563:37say: 0 kl4|us: !!alias kl4

and I need to extract some information. I'm trying with this Python code:

 import re
 x = "1563:37say: 0 kl4us: !!alias kl4"
 res = re.search( r"(?P<say>say(team)?): (?P<id>\d+) (?P<name>\w+): (?P<text>.*)",x)

 slot= res.group("id")
 text = res.group("text")
 say = res.group("say")
 name = res.group("name")

This code works fine. Why if I have a character | or * into my string this regexp doesn't work?

For example:

 import re
 x = "1563:37say: 0 kl4|us: !!alias kl4"
 res = re.search( r"(?P<say>say(team)?): (?P<id>\d+) (?P<name>\w+): (?P<text>.*)",x)

 slot= res.group("id")
 text = res.group("text")
 say = res.group("say")
 name = res.group("name")

Anyone can help me?

Thanks a lot

1
  • @Wessie The characters mentioned appear in the data, not the regex. Commented Oct 17, 2012 at 17:59

1 Answer 1

5

Based on where you added the "|", it looks like you expect "|" and "*" to be matched by \w, but \w only matches letters, digits, and "_". To match those characters as well, change the \w to [\w|*]:

res = re.search( r"(?P<say>say(team)?): (?P<id>\d+) (?P<name>[\w|*]+): (?P<text>.*)",x)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much and sorry for late. Your code works fine. Thanks again

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.