Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C# Regex line adaptation to remove more characters
New Posts  All Forums:Forum Nav:

C# Regex line adaptation to remove more characters

post #1 of 3
Thread Starter 
I wanted to remove unwanted characters in a string and have found the following lines on the internet and have adapted them to my program. The problem now is that I want to adapt it to remove more characters but I don't understand it enough to do that. So far I've discovered that the first part of the bit in brackets contains the characters I want to keep in the string, and I assume the second part contains the characters I want to remove. I want to make this remove spaces as well, as currently I'm using a separate line to remove them via another method but I have no idea where you add that to this.
Code:
Regex reg = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
                word = reg.Replace(word, String.Empty);

tl;dr: I want to adapt this to remove spaces as well. rolleyes.gif
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
post #2 of 3
remove the space from between the brackets
Code:
:[^a-z0-9 ]
you have the space as part of the match you want to keep, delete it.

and this
Code:
Regex reg = new Regex("(?:[^a-z0-9]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);

could be shortened to
Code:
Regex reg = new Regex("[^a-zA-Z0-9]");
if all you want is upper and lower case letters and numbers
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #3 of 3
Thread Starter 
Quote:
Originally Posted by tom.slick View Post

remove the space from between the brackets
Code:
:[^a-z0-9 ]
you have the space as part of the match you want to keep, delete it.

and this
Code:
Regex reg = new Regex("(?:[^a-z0-9]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);

could be shortened to
Code:
Regex reg = new Regex("[^a-zA-Z0-9]");
if all you want is upper and lower case letters and numbers

Cheers thumb.gif

I thought I could drop all of the end options too but I just didn't try it tongue.gif Now I'm just going to edit this line and my software dev assignment program is done biggrin.gif
Edited by Scott1541 - 4/20/14 at 3:32pm
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C# Regex line adaptation to remove more characters