Robby Smet
Robby Smet

Reputation: 4661

Visual studio find & replace with regex, replace space with underscore

I have a large list of text in c# that in want to use as an enum list. For example:

Zone Status Message
Zones Snapshot Message
Partition Status Message
Partitions Snapshot Message
Supported transition message flags
System Status Message
X-10 Message Received
Log Event Message
Keypad Message Received

Now I want to use the find and replace dialog in visual studio to add underscores in all the words instead of a space, for example

Zone_Status_Message
Zones_Snapshot_Message

How can I achieve this using regex or wildcard? This would really save me a lot of time.

Thanks in advance.

EDIT:
The words can also have spaces and the front and back, due to a poorly formatted source document where I get the text from. So the underscore should only be added when the space is between 2 words.

Upvotes: 1

Views: 3821

Answers (2)

MikeM
MikeM

Reputation: 13641

In VS 2010, to replace a space between alphanumeric characters you could use

Find What {:a} {:a}
Replace With \1_\2

Make sure Use Regular expressions is checked.

Replace All

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336378

If you only want to match spaces between alphanumeric words, search for > < and replace all with _.

In VS regexes (until version 2010), > means "the position at the end of a word" and < means "the position at the start of a word".

In VS 2012 and up, that regex could be written as \b \b.

Upvotes: 1

Related Questions