0

Considering the following string

"Nr.: &N|1|2|3|4|5|6|7 / 8|9|10|11|12 / 2014"

I would like to get the following match:

"&N|1|2|3|4|5|6|7 / 8|9|10|11|12 "

Though the regex pattern &N\|(.*|$)\s matches:

"&N|1|2|3|4|5|6|7 / 8|9|10|11|12 / "

But after the last vertical line, I would like to match only until the next and not the last space.

Any ideas of how I could solve this?

Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
  • There are many ways to match it, including `&[^/]*/[^/]*`. But how strict do you want the regex to be? – nhahtdh Jan 15 '14 at 11:16

3 Answers3

2

One of many ways you could achieve this.

&[^&]*(?=/)

See Live demo

hwnd
  • 69,796
  • 4
  • 95
  • 132
2

If there is always going to be a space and a slash and a space and a year at the end you can do this:

&N\|(.*|$)\/

The match will be:

"1|2|3|4|5|6|7 / 8|9|10|11|12 "

See it in action

e h
  • 8,435
  • 7
  • 40
  • 58
2

Lots of ways to do this. My personal creation:

&.*\|[^ ]*

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56