1

I have a scenario where I need to fetch a particular pattern from the string using regular expression.

The string looks like below:

${text} = Slot 0   l  5  3  24+6 
          Slot 1   l  3 16  10
          Slot 3   l  4  3  32
          Slot 8   l  2  3 
          Slot 9   l  1  3

Here, I need to fetch only

Slot 0 Slot 1 Slot 3 Slot 8 Slot 9

How do I do this?

I have tried using the keywords 'Replace String Using Regexp' and 'Get Regexp Matches' for the same.

${text}=  String.Replace String Using Regexp  ${response}   [^Slot\\s+\\d], ${EMPTY} 

The result was:

${text} = Slot 0 l 5 3 24+6 Slot 1 l 3 16 10 Slot 3 l 4 3 32 Slot 8 l 2 3 Slot 9 l 1 3 –

And, Get Regexp Matches gives the below result:

${matches}=   String.Get Regexp Matches  ${response}    [Slot\\s+\\d] 

The result:

${matches}=   ['S', 'l', 'o', 't', ' ', '0', ' ', ' ', ' ', 'l', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '5', ' ', '3', ' ', ' ', '2', '4', '+', '6', '\r', '\n', 'S', 'l', 'o', 't', ' ', '1', ' ', ' ', ' ', 'l', ' ', ' ... –
8
  • 4
    What have you tried, show the effort you've put in solving it yourself. Don't just expect it'll be solved by someone else for you - the community will help and guide, but the purpose is hardly doing something instead of you. I'd recommend using this site trying out the different options - regex101.com , select the python flavor in it. Commented May 18, 2017 at 7:12
  • I have tried in number of ways. I have also used pythex.org also for this. When I have not found the solution for this, I wanted to know if there is some one who can have a solution for it. Commented May 18, 2017 at 7:30
  • You're on the right track, but spin it otherwise - use the keyword Get Regexp Matches to get just the substrings you're interested in; the regexp is also ok, just don't negate it like in your removal approach. And add your comment, plus any further results in the question's body ;) Commented May 18, 2017 at 7:38
  • 1
    In your second attempt, remove the square brackets, and you're there! From regex101 hints - [] Matches a single character from the list - you don't need that, you want a match on the whole substring. Commented May 18, 2017 at 7:40
  • 1
    Please delete your KEYWORD comments (they are fairly unreadable) and add them into the question via the edit feature, thanks. Commented May 18, 2017 at 8:26

1 Answer 1

2

The solution is just to remove the square brackets used for the regular expression in 'Get Regexp Matches' keyword.

i.e., Use Slot\s+\d+ instead of [Slot\s+\d+]

This is because [] Matches a single character from the list and my requirement was to fetch the whole substring.

Thanks @Todor

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.