views:

70

answers:

2

I have a file with lots of text editing using NotePad++.

for example

<span class="italic">some text</span><span class="bold">another text or some text</span>

I would like to use NotePad++'s regex replace to replace

<span class"italic>some text</span> to <i>some text</i> and <span class="bold">another text or some text</span> to <b>another text or some text</b>

I'm able to match the span text however How to replace them with NotePad++

Find <span class="italic">text12312</span> and replace it with <i>[a-zA-Z]*</i> will actually put the "[a-zA-Z]*" text into replaced string and not "text12312".

+1  A: 

Use a regex like: <span class="italic">([\w\s\d]+)</span>

and replacement like: <i>$1</i>

The important point here is to create a matching group for your text by surrounding it in brackets i.e. ([\w\s\d]+) which matches one or more:

  • \w word chars
  • \s space chars
  • \d numeric chars

Now in your replacement string, reference the first and only matched group with $1.

krock
`<span class="italic">some text and <span class="custom"> some more </span> text </span>`
Amarghosh
@Amarghosh, as stated in the question, OP only wants to match `[A-Za-z]` in between the span tag which precludes the need to check for nested spans.
krock
He uses `[a-zA-Z]*` and his sample text contains digits - I was just suggesting that if his text is large enough, sooner or later he'll realize that its not a good idea even to search-n-replace html with regex
Amarghosh
Are yo sure it is working in notepad++? it doesn't work with me.
VOX
NPP uses `\1` in the replacement string, not `$1` like most other regex tools. Also, NPP doesn't support class shorthands (`\w`, `\s`, etc.). If it did support them, `\d` would be redundant because `\w` is equivalent to `[A-Za-z0-9_]`.
Alan Moore
Thank for your answering.
NgeLay
A: 

<span class="italic">([^<]+)</span> => <i>\1</i>

<span class="bold">([^<]+)</span> => <b>\1</b>

[^<]+ matches one or more of any character except <, and the parentheses capture it in group #1. \1 inserts the captured text into the replacement string.

Alan Moore