Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using regex or anything, I need to match any paragraph which contains at least one all caps word.

Then transform it into a header and capitalise all the content of the title. In the example below, the word Here should be changed into HERE. This includes accented characters.

How can I do this?

<P STYLE="margin-bottom: 0cm"><BR>
    </P>
</DIV>
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0cm; line-height: 0.42cm">
<FONT SIZE=4><B>TITLE Here</B></FONT></P>

I'm using Linux.

share|improve this question
    
Please include a larger example. Will all the relevant lines start with <FONT? Could the CAPS word be anywhere in the paragraph? Should the header be at the beginning of the paragraph? –  terdon Jun 5 '13 at 18:09
1  
Parsing [X]HTML with regexs is discouraged. If you insist on doing it, you probably need to tell us exactly what flavor of regexs you want to use. –  Scott Jun 6 '13 at 1:14
    
I'm not insisting on regex but don't know any other semi-automatic way to capitalise sentences. In this particular case the html file was done without class declarations. I went through the file and I noticed that all (or almost all) the headings have this in common: at least one all caps word. This is the reason behind my question. –  To Do Jun 6 '13 at 19:59

1 Answer 1

You can almost certainly do this using console commands, but this sounds like a one-off repair job rather than a regular chore so I would use my favourite graphical regex tool, the very handy open source programmers' text editor called jEdit. (See jedit.org for details.)

Once you've installed jEdit you can open the "Search and Replace" dialog by hitting Ctrl+F or by going to the "Search" menu and selecting the "Find" item.

In the "Search and Replace" dialog you need to select the "Search in" option you wish to use. "Current buffer" will change only the current file, "All buffers" will change all files currently open in jEdit, and the "Directory" option will allow you to change all files in the directory (which you specify in the bottom of the dialog) which match the filter you specify (such as *.html or *.php). The "Directory" option is very powerful, and is possibly the option you need, but be very careful with it and check very carefully that your search/replace does not cause unexpected changes by matching in ways you didn't consider.

Next you need to tick the "Regular expressions" box in the middle of the dialog.

Now type into the "Search for" textbox the regex you want to use to target the text which needs to be changed. If I understand your requirement, this regex pattern might do the job:

(?<=>)([^<]*\p{Lu}{2,}[^<]*)(?=<)(?!</script)

This pattern says "select text which is preceded by a > and which contains any number of characters which are not < then at least two consecutive uppercase characters, then any number of characters which are not <, and which is finally followed by a < but NOT by </script".

Then you need to select the "Return value of a BeanShell snippet" option and type into the "Replace with" textbox:

_1.toUpperCase()

This BeanShell snippet just says "the text captured in the first capture group matched by the regex, converted to uppercase".

I've tested this and it happily runs through an HTML file finding consecutive sequences of raw text which contain two-or-more consecutive uppercase characters. Be aware that any paragraph which contains something innocent like UK or USB will also be matched by this regex, so you might want to change the {2,} quantifier to {4,} so that there have to be at least four consecutive uppercase characters for a sequence to be matched.

Also make sure to test very carefully before you unleash this on a valuable file or set of files (and make sure you've taken a backup of all the originals first so you can restore them if things go awry and you don't notice until you've saved the changes).

share|improve this answer
    
The search worked pretty well but the script is not working. –  To Do Feb 2 at 14:15
    
Are you finding the same weird problem I was having (and assumed it was a quirk on my machine): that the "Replace" and "Replace/Find" buttons don't do anything, but the "Replace All" button does work? –  Arkanon Feb 2 at 16:57
    
Yes. Sortof… Not sure it did capitalise the text tough. –  To Do Feb 3 at 11:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.