1

The regular expression I am using:

%s??[a-z](:\d+|)(^\[)? 

The string is:

"%sprix is the %s[4] online %s[3]:6 pshopping %s:9 in %s" 

I am trying to find all %s from string except %s[4] and %s[3].

My output using the above regular expression is not giving expected results.

Expected Output is:

%s, %s:9, %s 

My Output is:

%s, %s[ , %s[, %s:9, %s 
3
  • What do you want to match in the above string? See regex101.com/r/i0dfOJ/1 Commented Nov 5, 2016 at 21:26
  • I have added kindly check it Commented Nov 5, 2016 at 21:37
  • Why do you need to parse an already formatted string? Commented Nov 5, 2016 at 21:45

1 Answer 1

2

You may use

%s(?!\[)(?::\d+)? 

See the regex demo.

Details:

  • %s - a percentage sign and s
  • (?!\[) - that is not followed with [
  • (?::\d+)? - an optional sequence of a : and one or more digits

The above regex will fail the match in all cases when %s is followed with [, e.g. in %s[text. If you only want to fail the match when %s is followed with [+number+], use %s(?!\[\d+\])(?::\d+)?.

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

1 Comment

FYI, this will fail the match in all cases when %s is followed with [, e.g. in %s[text. If you only want to fail the match when %s is followed with [+number+], use %s(?!\[\d+\])(?::\d+)?.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.