2

I would really appreciate some help with this. I am working with a large corpus of text that has been marked-up with XML script and I need to tidy some things up. More specifically, there are several hundred numbers in square brackets (i.e., like '[1234]') that I need to delete (brackets included). Each number is different and there can be anything from 3 to 5 digits in these numbers.

The second thing I need to do is to place XML tags around some other pieces of text. These are names in round brackets. I.e., like:

He (Mr. Smith) said...

Which I want to change to:

He <annot> (Mr. Smith) </annot> said...'.

How can I perform both of these changes?

I've already tried using Extended Mode and searching for '[.*]', but it returns no hits. When I try the same with RegEx I get lots of hits, but it seems to be searching for each and every square bracket in the corpus.

1

The square brackets are a special token in regular expressions ([a-z] means any character in the range a to z), so you'd have to escape them.

Your search expression would become \[\d+\] (with \d+ meaning one or more digits). However you want to replace this with the digits alone. You need to setup a capturing group, using parentheses, and reference it in the replace expression. Your settings become:

  • Search for: \[(\d+)\]
  • Replace with: \1

As for your second problem, I'm not sure I understand it properly with the context you give... if only names are between parenthesis, you can just replace \([^\)]+\) with <annot> \0 </annot>, or maybe \(Mr\. [^\)]+\) if the names are preceded by Mr..

To get into details, the way it works is so:

  • the parenthesis you search for have to be escaped (as they are normally used to define capturing groups, so you use \( and \)
  • [^...]+ means one or more characters which is not ...
  • your search expression is therefore an opening parenthesis, followed by any characters which is not a closing parenthesis (so it should match the names), followed by a closing parenthesis.
  • \0 represents the whole searched expression in the replacement area

If you have a list of those names to replace, then you'd better use \(whatever name\) as the search expression, and if you don't, then it's impossible, as you can't make a regexp "guess" what's a name and what's not.

| improve this answer | |
  • 2
    Also, you need to expilictly search using regex. Extended mode only does \t for tabs, \r \n for new lines, etc. – LPChip Jul 18 '14 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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