![]() |
#1 |
MrsUndertaker
![]() Posts: 17
Karma: 10
Join Date: Sep 2011
Device: Galaxy Tab S2 9.7
|
Help with a search & replace
I want to locate any authors with two or more initials with periods, each separated by a space, like "A. B." or "C. D. E."
I've worked out a regex that will find them, but I can't figure out how to remove the space between two initials. Help? |
![]() |
![]() |
![]() |
#2 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 3,130
Karma: 91256
Join Date: Feb 2008
Location: Germany
Device: Cybook Gen3
|
I don't understand what you want to remove. Could you post some examples of what values you currently have in your library and what you want to change them to?
Spaces can best be matched by using either a space or \s for general whitespace matching. |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,343
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Search field: authors
Search for: (\w\.) (?=\w\.) Replace with: \1 |
![]() |
![]() |
![]() |
#4 | |
MrsUndertaker
![]() Posts: 17
Karma: 10
Join Date: Sep 2011
Device: Galaxy Tab S2 9.7
|
Quote:
That does exactly what I wanted - thank you very much. But I don't understand why it works. Enlighten me, please? |
|
![]() |
![]() |
![]() |
#5 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Code:
Search for: (\w\.) (?=\w\.) Replace with: \1 \w\. is a single word character followed by a period (the \. means a period, while the dot alone without the escape backslash is a wild card for any character.) So "(\w\.) " means a single word character followed by a period followed by a space (note the space there). (?=\w\.) means to only find "a single word character followed by a period followed by a space" if the space is followed by "a single word character followed by a period". The pattern (?= is a positive lookahead assertion. It lets the preceding match only when the following matches, but the lookahead part doesn't "eat up" any of the string. For example, the regex "Isaac (?=Asimov)" will match "Isaac " only if it’s followed by "Asimov". Last edited by Starson17; 10-14-2011 at 02:27 PM. |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
MrsUndertaker
![]() Posts: 17
Karma: 10
Join Date: Sep 2011
Device: Galaxy Tab S2 9.7
|
Thanks for all the details. I still don't understand the "Replace with". How does that remove the space?
|
![]() |
![]() |
![]() |
#7 | |
Well trained by Cats
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 30,914
Karma: 60358908
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
Quote:
\1\2 Since the spac between other match elements is not in side a reference (), it will be lost when replacing only the 2 back references. |
|
![]() |
![]() |
![]() |
#8 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 13,324
Karma: 78876004
Join Date: Nov 2007
Location: Toronto
Device: Libra H2O, Libra Colour
|
Quote:
Since the (?=\w\.) is as chaley says "(?= is a positive lookahead assertion. It lets the preceding match only when the following matches, but the lookahead part doesn't "eat up" any of the string." this means the only characters "consumed" by the reg ex. are the initial sequence "(\w\.) " and that is replaced by the (1) which is that initial \w\. sequence. |
|
![]() |
![]() |
![]() |
#9 |
US Navy, Retired
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,890
Karma: 13806776
Join Date: Feb 2009
Location: North Carolina
Device: Icarus Illumina XL HD, Kindle PaperWhite SE 11th Gen
|
Since he already stated that the S&R worked flawlessly I'm guessing the \2 isn't required.
Good explanation. |
![]() |
![]() |
![]() |
#10 | ||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,343
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
No. The second parenthesized expression (the positive lookahead) does not create a group that can be back referenced. Adding the \2 will generate an error, because there is only one group.
Quote:
Quote:
Note that "leftmost-overlapping" does not imply either "adjacent" or "leading". It simply means that the input string is scanned from left to right. For example: regarding adjacent, there is no requirement that there be only one set of initials. Given the rather bizarre author name "A. B. Someword C. D. Lastname", the expression will match the A. and the C., resulting in "A.B. Someword C.D. Lastname". Regarding leading: the name "Joe A. B. Smith" will be changed to "Joe A.B. Smith". |
||
![]() |
![]() |
![]() |
#11 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Quote:
Simple ![]() |
|
![]() |
![]() |
![]() |
#12 |
MrsUndertaker
![]() Posts: 17
Karma: 10
Join Date: Sep 2011
Device: Galaxy Tab S2 9.7
|
Thank you all for the awesome replies. It was all very helpful!
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Search & Replace :help: | krussell | Calibre | 3 | 08-02-2011 04:45 PM |
Search & Replace/Regex help!! | millertime13 | Conversion | 4 | 07-22-2011 02:40 AM |
Search & Replace Suggestion | Philosopher | Calibre | 6 | 12-31-2010 11:55 AM |
Search & Replace | Pat Nickholds | Sigil | 2 | 10-21-2010 11:18 PM |
Search & replace TEXT | ToeRag | Calibre | 3 | 04-10-2010 01:44 PM |