0

I want to get two specific words using regex after a word

So I have the following text

code: 'dev-foo-1', text: foo, bla:, blabla, ..., tokenId: '1234566343434', accountId: '123456789',...

I want from this text only the code dev-foo-1 and the account id 123456789 But I am having problem to select those two.

I tried to do something like

code:'\([a-z]{3})([a-z]+)([0-9])'\accountId: \'([0-9])'

which it's wrong.

Barmar
  • 741,623
  • 53
  • 500
  • 612
pralxx
  • 65
  • 6

1 Answers1

1

(Note that I've ignored backslashes in your code because they appear to be inconsistently applied – you'll need to add slashes before each single-quote if you're using single-quoted strings to delimit your patterns)

If code always appears before accountId then you're pretty close to an answer. You're missing a space between code and the code, and also the dashes between the sections of the code:

code: '([a-z]{3}-[a-z]+-[0-9])'

There can then be any characters at all before the accountId, so you'll need .* to match them. Actually, you can use .*? to be non-greedy.

Finally, your accountId currently matches only a single digit, but you'll presumably want to match one or more with [0-9]+:

code: '([a-z]{3}-[a-z]+-[0-9])'.*?accountId: '([0-9]+)'

... and we are done.

motto
  • 2,888
  • 2
  • 2
  • 14