Mikael
Mikael

Reputation: 1309

Regexp how to set any number of characters but '|' is required?

This is the string:

[randstr: sdfsdfds|dsfsdf sdfds 43rew|fds, 1-1]

This string may occurs in a text. This is my regexp I try:

`\[(randstr|randstrFixed):(\s+)?\|(.+)(\s+)?,(\s+)?[\d]+-[\d]+]`

The main goal to set to the pattern that: sdfsdfds|dsfsdf sdfds 43rew|fds - here may be any chars but at least one '|' is required. Such requirement because in text may occur another similar string like [randstr: A-Z, 2-4]

More explanation:

https://regex101.com/r/1BNzA4/2/ - here is example text and regexp from @Wiktor Stribiżew

There u can text is changed, now the 'pattern' begins from [-> so in regexp I have to tell it that [-> must mot occur.

Upvotes: 1

Views: 69

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

You may use

\[randstr(?:Fixed)?:\s*[^][|]*\|[^][]*,\s*\d+-\d+]

See the RE2 regex demo

Details

  • \[ - a [ char
  • randstr(?:Fixed)? - randstr or randstrFixed
  • :\s* - a colon and 0+ whitespaces
  • [^][|]* - 0+ chars other than [, ] and |
  • \| - a | char
  • [^][]* - 0+ chars other than [ and ]
  • ,\s* - a comma and 0+ whitespaces
  • \d+-\d+ - 1+ digits, -, 1+ digits
  • ] - a ] char.

Upvotes: 2

Related Questions