Regex function that only affects strings with specific notation

58 Views Asked by At

I am trying to manipulate text output from a web scrape I have done. I want a regex function that ONLY for strings that include the notation "-" to remove words from after this in each string.

The function '\s*(.*)(?=-)' works, but removes all text from strings that don't have this notation.

How can I write a function that only affects strings with the "-" notation?

To give context, I am mining real estate agency information, and some entries (not all ) include the specific branch which is delineated using a "-"

However, for the country wide statistical analysis, I don't want the names of the specific branches as this makes it more difficult to format the data effectively.

Attached is a screenshot detailing what the input/desired output should be. enter image description here

1

There are 1 best solutions below

1
Pushpesh Kumar Rajwanshi On

You can use -.*$ regex to find lines that contain a hyphen and optionally some text after that and replace all that with empty string.

Check this demo

Let me know if this works for you.