1

Is there any way to get all string (include other sign like -,) before a given specific sign (in this case '@' sign)?

Example domain\username@abc I want to get "domain\username" so I use expression (.+)@ and it work.

but in same time there will be some input witch are without @abc only "domain\username" ,So it don't need to be split, (.+) will work

But can't figure out a regex expression that can match both ,

  1. Domain\username@abc.com return Domain\username
  2. Domain\username return Domain\username

IN THE SAME GROUP

My solution is make a if-else before regex expression so it can decide is string contains @ sign, so I can apply different expression for both situations.

I had tried

  1. Use or (|) to make end of string are @ or word boundary :(.+)(@|\b) ->it always match \b so it will not stop at '@' sign
  2. Make @ sign match 0 or 1 times : (.+)@{0,1} ->>I don't understand Why this not work.But it not work in regex101.com ,it will always match full string
LocoOcelot
  • 65
  • 7
  • your 2. try match all, because of greedy Expression https://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions – user3104267 Mar 18 '19 at 16:32

1 Answers1

2

You may use

^[^@]+

See the regex demo

It will match one or more chars other than @ ([^@]+) at the start of the string (^).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    It work perfectly . And then I make something different Like https://regex101.com/r/g3dCpC/1 with new idea ! – LocoOcelot Mar 20 '19 at 05:54