2

I really am not getting how I can match just a single instance of both a whitespace or a dash. I am able to get some results but still unable to get the desired one. I have actually tried different configurations from regexr.com and what not but still cant get it to work.[regex padawan here]

Shell: Powershell
Data : "test--asd :45; wth---notcool:  69"

Index   0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32
Value   t   e   s   t   -   -   a   s   d       :   4   5   ;       w   t   h   -   -   -   n   o   t   c   o   o   l   :           6   9

Test 1 regex = '([\s\-])\1?'

Test 2 regex = '([\s\-]){1}'

Testing 1         Testing 2       Desired to be REMOVED 
Index   Value     Index Value     Index Value
-----   -----     ----- -----     ----- -----
4       --        4     -         5     -
9                 5     -         9 
14                9               14    
18      --        14              19    -
20      -         18    -         20    -
29                19    -         30        
                  20    -       
                  29            
                  30            

I think im missing something really basic here or fundamental, but my main goal with this is that I want to use regex to basically reduce the spaces and dashes to a single instance each so that the string isn't messy.

******* EDIT (Added explanation) *******

Basically, what I need to do is to get rid of the white-spaces and to transform the redundant dashes to a single dash. This is why I thought I could create a regex to select(get the index) the white-spaces and the extra dashes(starting from the 2nd consecutive dash) and replace them as nothing(''). Sorry for the confusion, having a difficulty in stating the issue and the correct approach.

So the main goal is reflected below, I jsut need to get rid of the ones in yellow:

Final desired output

CodeNagi
  • 419
  • 2
  • 10
  • What is your expected output? – Tim Biegeleisen Mar 22 '18 at 03:57
  • Hey @TimBiegeleisen! its labeled as `"Desired Output"` – CodeNagi Mar 22 '18 at 04:03
  • So you just want to replace any instance of 2 or more - with one -? And the same for spaces? – EBGreen Mar 22 '18 at 04:27
  • was aout for lunch folks sorry!What TessellatingHeckler said was actually close buddy. I just need to change the repeating Hyphens(dash) to be a single dash, and remove the spaces so I thought I could just look for a single dash with a nearby dash and the white space(which i see isn't the correct logic). @EBGreen – CodeNagi Mar 22 '18 at 06:51
  • 1
    "*having a difficulty in stating the issue*" - if only you had a small, precise language for describing text patterns to help you ;-) – TessellatingHeckler Mar 22 '18 at 08:20

1 Answers1

3

I think you want:

  • spaces replaced with nothing
  • multiple dashes reduced to a single dash

That means any regex which matches (space or dash) is going to have to have a replacement value which varies, based on whether it matches a space or a dash. Possible, but annoying.

Instead, I would rephrase it to make the replacement value the same for both cases - empty string - and that makes things which should be replaced:

  • One or more dashes following a dash get replaced with nothing, the leading dash stays
  • space

Which would be:

PS C:\> "test--asd :45; wth---notcool:  69" -replace '(?<=-)-+|\s'
test-asd:45;wth-notcool:69

That is, (?<=-) for a lookbehind matching a dash which will stay in the string, -+ for one or more dashes after it (these will be replaced with nothing). | or. \s a whitespace character.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • Apologies for the confusion , but this is really close! i've added more information for clarity. I know I can just chain another `Replace` method to the result to change the space to nothing but I wanted to explore regex if its capable of replacing multiple characters at the same time like my desired output. @TessellatingHeckler – CodeNagi Mar 22 '18 at 07:09
  • 1
    THANKS A LOT MAN!! @TessellatingHeckler – CodeNagi Mar 22 '18 at 09:54
  • hey mannn would it be okay if I let you read my most updated question? Sorry to bother you. @TessellatingHeckler – CodeNagi Apr 01 '18 at 16:05