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

  • SEARCH
  • Home
  • 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 7929497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:05:50+00:00 2026-06-03T20:05:50+00:00

I have this string: City – This is some text. This is some more

  • 0

I have this string:

City – This is some text. This is some more – and continues here.

I would like to split the string at the first ‘ – ‘ to find ‘city’ (just a sample word, it can be other words as well). Plus to find the rest of the text after ‘ – ‘.

I constructed this expression:

(^[\D\W\S]*)( - )([\D\W\S]*)

But this finds the last occurrence of ‘ – ‘ instead of the first one.

How can I stop at the first occurrence ?

  • 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-06-03T20:05:51+00:00Added an answer on June 3, 2026 at 8:05 pm

    The simplest solution would be to explicitly forbid the dash to be part of the first group:

    ^([^-]*) - (.*)
    

    Explanation:

    ^        # Start of string
    ([^-]*)  # Match any number of characters except dashes
    \ - \    # Match a dash (surrounded by spaces)
    (.*)     # Match anything that follows
    

    However, this would fail if your string could contain a dash in the first group (just not surrounded by spaces). If that’s the case, then you can make use of lazy quantifiers:

    ^(.*?) - (.*)
    

    Explanation:

    ^        # Start of string
    (.*?)    # Match any number of characters, as few as possible
    \ - \    # Match a dash (surrounded by spaces)
    (.*)     # Match anything that follows
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this string stored in a variable: IN=bla@some.com;john@home.com Now I would like to
I have this string in vb.net. I would appreciate if you can let me
If I have the name of a database table like this: string tableName =
I have a object graph that is setup like this Clients string Name List[Address]
I have this situation Session session = sessionFactory.openSession(); ArrayList<String> city; // in city array
I have this DomainClass: package cm class XXX{ String city String website static constraints
I have some data in MongoDB that looks like this: { name: Steve, location:
This is the following. Normally, when i have a city, i'm using some javascript
I have this string: 123-456-7 I need to get this string: 1234567 How I
I have this string: \Blah \'Blah\' Blah\ . There is another string inside it.

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.