sazr
sazr

Reputation: 25928

Grab Everything Except the Hyphen

Using a regular expression I want to grab all text before the -. My below regex can successfully grab the text I want but it also grabs the hyphen. How can I stop this?

/(.*) -

For "abc - def" it returns "abc -" but I am attempting to get "abc ". I am performing this regex in Python.

Upvotes: 1

Views: 71

Answers (4)

Zach S.
Zach S.

Reputation: 130

All you want to do is grab everything up to the first - and store it into a group. Depending on the regex you are using (perl style or otherwise)

You would do something similar to what you have, but I would lazy look for the first match of the - and group everything before it.

That is to say: (.*?)- Would Return: "abc -" But this would contain the group of "abc " and a group of the whole thing "abc -" You merely want the group without the - and can access it by your language's group modifier.

https://regex101.com/r/cH6gO8/1

For python:

>>> re.match('(.*?)-', 'abc - cba').group(1)


In [1]: mystring = "abc - cba"                                                                                                                                                  

In [2]: import re                                                                                                                                                               

In [3]: re.match(r'(.*?)-', mystring).group(1)                                                                                                                                  
Out[3]: 'abc '

Upvotes: 0

hwnd
hwnd

Reputation: 70722

I am performing this regex in Python.

As my comment stated above, reference the group index to grab the match result only.

>>> re.match('(.*)-', 'abc - def').group(1)
'abc '

But, I see no need to really use a regular expression here:

>>> 'abc - def'.split('-')[0]
'abc '

Upvotes: 3

David S.
David S.

Reputation: 11148

Excluding the hyphen does not work for your?

([^\-]) -*

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

Your regex is correct, you just need to print the group index 1. But it would print abc not abc<space>.

If you want to match the chars which exists before hyphen without the hyphen then you could use positive lookahead.

.*?(?=-)

In python,

>>> import re
>>> re.match(r'(.*)-', "abc - def").group(1)
'abc '
>>> re.match(r'.*(?=-)', "abc - def").group()
'abc '

Upvotes: 2

Related Questions