Help with Regex

Hi,

I need help with a Regular Expression, I need to split the text on "- "
on get the first element.

“Web Application / QA Developer (Mid-Senior Level) - BFG Interactive -
(Hilton Head Island, SC) - FullTime”

I tried this, but it doesn’t work: /(.*) - /i

Gregory

On Sun, Jul 24, 2011 at 5:11 PM, Gregory Ma [email protected]
wrote:

I need help with a Regular Expression, I need to split the text on "- "
on get the first element.

Try String#split:

ary = “I want this - not that.”.split(" - ")
=> [“I want this”, “not that.”]
ary[0]
=> “I want this”


Phillip G.

phgaw.posterous.com | twitter.com/phgaw | gplus.to/phgaw

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

I know I can use split but I wanted to do it with a regex

On Mon, Jul 25, 2011 at 12:26:54AM +0900, Phillip G. wrote:

On Sun, Jul 24, 2011 at 5:11 PM, Gregory Ma [email protected] wrote:

I need help with a Regular Expression, I need to split the text on "- "
on get the first element.

Try String#split:

ary = “I want this - not that.”.split(" - ")
=> [“I want this”, “not that.”]
ary[0]
=> “I want this”

. . . and if you want to split it into only two elements even if there
are multiple instance of the split substring, use the optional limit
argument:

>> ary = "I want this - not that - nor the other.".split(' - ', 2)
=> ["I want this", "not that - nor the other."]
>> ary[0]
=> "I want this"
>> ary[1]
=> "not that - nor the other."

On Mon, Jul 25, 2011 at 12:11:03AM +0900, Gregory Ma wrote:

Hi,

I need help with a Regular Expression, I need to split the text on "- "
on get the first element.

“Web Application / QA Developer (Mid-Senior Level) - BFG Interactive -
(Hilton Head Island, SC) - FullTime”

I tried this, but it doesn’t work: /(.*) - /i

I don’t know what code you’re actually using with this regex. As
already
mentioned in other email responses (one of them from me), you might want
to try using the split method with an explicit string rather than a
regex. If you want to use a substitution regex, though, you might do
something like this:

>> foo = 'I want this - not that - nor that.'
=> "I want this - not that - nor that."
>> foo.sub(/ - .*/, '')
=> "I want this"

On Mon, Jul 25, 2011 at 12:11:03AM +0900, Gregory Ma wrote:

Hi,

I need help with a Regular Expression, I need to split the text on "- "
on get the first element.

“Web Application / QA Developer (Mid-Senior Level) - BFG Interactive -
(Hilton Head Island, SC) - FullTime”

I tried this, but it doesn’t work: /(.*) - /i

Try the non-greedy version, /(.*?) -/i

Dan

I need help with a Regular Expression, I need to split the text on "- "
on get the first element.

str = “part 1 - part 2 - part 3”

str =~ /-/
p $`

OR

p (/-/.match(str)).pre_match

Harry