I need to generate a regex that will match the following format:
-1 LKSJDF LSAALSKJ~
Syjsdf
lkjdf
This block may contain multiple characters including digits, colons, etc. Any character other than a tilde.
~
I'm currently using this:
/(-\d|\d)\s([^$\~][a-zA-Z\s]*)\~\n/s
Which matches the first line fine. I need to capture the -1 through 60 that begins the pattern, the words after the space and up until the first tilde. I then need to capture all of the text BETWEEN the tildes.
I'm not the strongest with regex in the first place, but I'm having trouble getting this to work without also capturing the tildes.
You can use
'/^(-?\d+)\s+([^~]*)~([^~]+)~/m'
See demo
The regex matches:
^
- start of a line (due to /m
modifier ^
does not match start of string any longer)(-?\d+)
- (Group 1) a one or zero -
followed with one or more digits\s+
- one or more whitespace symbols (to only match tab and regular spaces, use \h+
instead)([^~]*)
- (Group 2) zero or more characters other than a ~
(you can force to match these characters on the first line only by adding a \n\r
to the negated character class - [^~\n\r]
)~
- a literal leading tilde([^~]+)
- (Group 3) one or more characters other than a tilde~
- a literal trailing tildeIf you need to only match these strings if the number is an integer between -1 and 60, you can use
'/^(-1|[1-5]?[0-9]|60)\s+([^~]*)~([^~]+)~/m'
See another demo
Here, the first group matches integer numbers from -1 to 60 with (-1|[1-5]?[0-9]|60)
alternation group. -1
and 60
match literal numbers, and [1-5]?[0-9]
matches one or zero (optional) digit from 1 to 5 (replace with [0-5]?
if a leading zero is allowed) and then any one digit may follow.