I never tried regex before today, and I like it so far, but I’m lost on some things.
I have a string that looks like this:
Type OtherType ThirdType - SubType AnotherSubType QuiteTheType
I want two regex, both care about the ‘-‘ character.
First I want all words before that character, then all words after it. I will be using Ruby’s gsub to turn them into an array of strings, two arrays, which is why I need two regex expressions.
So far I have this: ([a-zA-z]{1,}) (?=-) but that only gets me the word right before the dash, I.E. ThirdType.
If I just use ([a-zA-z]{1,}) I get all words highlighted, but that includes the ones AFTER the - which I don’t want yet.
How can I get all occurrences of [a-zA-z]{1,} that happen before - but not necessarily IMMEDIATELY before it?
I don’t know exactly how Ruby’s regex implementation works, but the following regex in Perl should get you what you want:
For example:
produces
ETA: To explain what’s going on, the initial
^denotes the beginning of the line and the ending$denotes the end of the line. So,^([a-zA-Z\s]+)starts at the beginning and (greedily) matches all of the words from the beginning of the line up until the space before the dash (which is escaped by a backslash, since-is a reserved character in most regex implementations). Likewise with([a-zA-Z\s]+)$.