Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6092175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:24:45+00:00 2026-05-23T12:24:45+00:00

I never tried regex before today, and I like it so far, but I’m

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:24:45+00:00Added an answer on May 23, 2026 at 12:24 pm

    I don’t know exactly how Ruby’s regex implementation works, but the following regex in Perl should get you what you want:

    /^([a-zA-z\s]+) \- ([a-zA-Z\s]+)$/
    

    For example:

    perl -e '$_="Type OtherType ThirdType - SubType AnotherSubType QuiteTheType";
    if(/^([a-zA-z\s]+) \- ([a-zA-Z\s]+)$/){print "$1\n";print "$2\n";}'
    

    produces

    Type OtherType ThirdType
    SubType AnotherSubType QuiteTheType
    

    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]+)$.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I have written some basic generic webservices before but I have never tried
Hi I have a difficult regex problem that I have tried and have a
I have never tried a custom session handler so far in PHP, and with
Ive been working with Python and C++ together for some time, but never tried
i have never tried something like this so i need your help. i have
I've tried implementing this example , but the progress never displays. My page doesn't
Never sure where to place functions like: String PrettyPhone( String phoneNumber ) // return
Never ran into this problem with jQuery before. I have the following: $(document).ready(function() {
Ive never heard of this before, and I have been coding in PHP for
I never did any serious Java coding before, but I learned the syntax, libraries,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.