473,396 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with regex

Hallo folks,

Just started to use regex a bit with c#.
Want to do the following perhaps somebody have a tip.

1) want to remove trailing and leading whitespaces from the string.
Tried this one here:
string delim = " \t\r\n";
to = from.Trim (delim.ToCharArray ());

2) remove all \t and \r from the string and replace them through blanks
?.
3) remove all spaces which are more than one.
(only one blank should be between each words)
(1 2 3 4 5 -> 1 2 3 4 5)
Tried this one here:
to = Regex.Replace (from, "[ ]{2,}", "", RegexOptions.None);

Thanks
Reg. Andreas


Nov 16 '05 #1
1 1315
Andi Reisenhofer <so***@aon.at> wrote:
Hallo folks,

Just started to use regex a bit with c#.
I don't think that your first two examples need regular expressions at
all...
Want to do the following perhaps somebody have a tip.

1) want to remove trailing and leading whitespaces from the string.
Tried this one here:
string delim = " \t\r\n";
to = from.Trim (delim.ToCharArray ());
That should work fine - could you give an example of it not working?
2) remove all \t and \r from the string and replace them through blanks
?.
to = from.Replace ('\t', ' ').Replace ('\r', ' ');
3) remove all spaces which are more than one.
(only one blank should be between each words)
(1 2 3 4 5 -> 1 2 3 4 5)
Tried this one here:
to = Regex.Replace (from, "[ ]{2,}", "", RegexOptions.None);


I think you mean

to = Regex.Replace (from, "[ ]{2,}", " ", RegexOptions.None);

That seems to work for me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
2
by: Luhar | last post by:
After much scouring of information on Regular Expressions from books and the web, I've come up with the this handy little Regex to parse links from HTML: ...
2
by: Michael R. Pierotti | last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$") Dim m As Match = reg.Match(txtIPAddress.Text) If m.Success Then 'No need to do anything here Else MessageBox.Show("You need to enter a...
1
by: hillcountry74 | last post by:
Hi, I'm stuck with this regular expression from past 2 days. Desperately need help. I need a regular expression that will allow all characters except these *:~<>' This is my code in...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
I'm trying to learn regex but since I've spent way too much time on the following "simple" case, there's obviously something I'm missing. I need to find all occurrences of a specific...
8
by: Alexander Vasilevsky | last post by:
This code Regex ge = new Regex(""); string format = ge.Replace("8 kBit/s, 8,000 Hz, Mono", "_"); returns "8 kBit_s_ 8_000 Hz_ Mono" and I need "8_kBit_s_ 8_000_Hz_ Mono"
4
by: Danny Ni | last post by:
Hi, The following code snippet is causing CPU to max out on my local machine and production servers. It looks fine on Expresso though. Regex rgxVideo = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.