Thread: Regex Question
View Single Post
Old 01-25-2011, 11:09 PM   #1
Archon
Zealot
Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!
 
Archon's Avatar
 
Posts: 110
Karma: 5176
Join Date: Dec 2010
Device: Mac OSX, iPad, iPod, & Nook
Regex Question

I am working on a document that has had all the italics formatting removed and replaced with _foo_.

There are two catagories of words:
Those with characters after the word and no space.
Those with spaces after the word in the middle of a sentence.

I can find all occurrances with:
_(.+)_

I can replace those with characters after the word with:
\\i \1\\i0
(so there is no space after the word for characters like periods and commas)

I can replace those with spaces after the word with:
\\i \1 \\i0
(note the space between the 1 and first backslash so there is still a space there)

Example words:
_You_
(with a space after) and

_genocide_.
with a period after.

OK
So how can I efficiently find all occurrences but yet replace the ones with spaces after the word with a the corresponding "\\i \1 \\i0" and replace the ones with a character other than a space after the word with "\\i \1\\i0" in a single pass of the file?

I seem to need something like an "if then else " statement in regex.

Thanks for stopping by.
Free beer tomorrow.
Archon
Archon is offline   Reply With Quote