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

···

--
Posted via http://www.ruby-forum.com/.

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"

···

On Sun, Jul 24, 2011 at 5:11 PM, Gregory Ma <g.marcilhacy@gmail.com> wrote:

--
Phillip Gawlowski

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

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:

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

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

Dan

···

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

--
Daniel Bye
                                                                     _
                                              ASCII ribbon campaign ( )
                                         - against HTML, vCards and X
                                - proprietary attachments in e-mail / \

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

> 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:26:54AM +0900, Phillip Gawlowski wrote:

On Sun, Jul 24, 2011 at 5:11 PM, Gregory Ma <g.marcilhacy@gmail.com> wrote:

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]