![]() |
#1 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 247
Karma: 89950
Join Date: Dec 2009
Device: Amazon Kindle 2
|
Search/Replace Question
Hello,
In the text that I'm working with all the words that should be italics are encased within underscores, and I was wondering how to replace it with italics, such as: Find: _text_ Replace: <i>text</i> Now under a wildcard search _*_ with minimal matching picks them up but I can't find how to replace it. Any help would be appreciated ![]() |
![]() |
![]() |
![]() |
#2 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 657
Karma: 64171
Join Date: Sep 2010
Location: Kent, England, Sol 3, ZZ9 plural Z Alpha
Device: Sony PRS-300, Kobo Aura HD, iPad (Marvin)
|
Use code view, In replace, select more, tick regular expression, tick minimal matching
search for _(.+?)_ repace with <i>\1</i> |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 247
Karma: 89950
Join Date: Dec 2009
Device: Amazon Kindle 2
|
Didn't work but thanks anyway.
|
![]() |
![]() |
![]() |
#4 |
Well trained by Cats
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 30,889
Karma: 59840450
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
|
![]() |
![]() |
![]() |
#5 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 247
Karma: 89950
Join Date: Dec 2009
Device: Amazon Kindle 2
|
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 61
Karma: 12096
Join Date: Sep 2010
Location: Tasmania
Device: Sony PRS 650
|
Hi,
The Find string is: Code:
_(.*)_ Code:
<i>\1</i> |
![]() |
![]() |
![]() |
#7 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 247
Karma: 89950
Join Date: Dec 2009
Device: Amazon Kindle 2
|
|
![]() |
![]() |
![]() |
#8 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 697
Karma: 150000
Join Date: Feb 2010
Device: none
|
You didn't need the ? in the search string, because that's the PERL way of indicating "minimal matching" and in sigil you get that by checking the minimal matching box. In other words, in the earlier search expression it was probably trying to search for a literal "?".
|
![]() |
![]() |
![]() |
#9 | |
Enquiring Mind
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
Quote:
"Non-greedy matching cannot be applied to individual quantifiers. Expressions like abc*?dd are not allowed. Use the Minimal matching option to set minimal matching on the whole expression." So I tried the regex without the "?": _(.+)_ - and with "Minimal matching" selected, and it worked fine. It was the "?" that was throwing Sigil's regex engine. Worth knowing for future reference. Edit: Heh! st_albert beat me to it while I was typing, in fewer words and with an explanation. ![]() |
|
![]() |
![]() |
![]() |
#10 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 697
Karma: 150000
Join Date: Feb 2010
Device: none
|
Heh. There are WAY too many dialects of regex for my taste.
By the way, is it true that folks wander around at geek gatherings like PenguinCon, showing each other regular expressions and saying "I'll bet you can't guess what this does"? ![]() |
![]() |
![]() |
![]() |
#11 |
Enquiring Mind
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
![]() I've never been to PenguinCon, but it'd almost be worth going just to see if they do! ![]() Most geeky gathering I've ever been to was a W3C technical conference, and if people were comparing regexes, it was by IM or email - absolutely everywhere you looked, inside and outside the conference hall and seminar rooms, people were typing away on their laptops. I rather suspect, though, that most of the stuff they were saying on IM was more along the lines of "Know anywhere we can go for a drink afterwards?" ![]() |
![]() |
![]() |
![]() |
#12 |
Jr. - Junior Member
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 586
Karma: 2000358
Join Date: Aug 2010
Location: Alabama
Device: Archos, Asus, HP, Lenovo, Nexus and Samsung tablets in 7,8 and 10"
|
I'm having a bad time with regex. I've read everything about it I can find but I can't seem to figure out how to build a replace string (I can't find any examples).
For instance, in this discussion what does "\1" mean and what does it do? Can anybody point me to a book or tutorial that uses specific examples to explain Regex? I have found quite a few tutorials that do this for the search function, but none for the the replace string. Thanks and regards - John P.S. Long, long ago, in a place far away, I wrote, as we called them then, a "string processor" for the PDP-8. I do understand the concept - its the syntax that befuddles me. |
![]() |
![]() |
![]() |
#13 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 657
Karma: 64171
Join Date: Sep 2010
Location: Kent, England, Sol 3, ZZ9 plural Z Alpha
Device: Sony PRS-300, Kobo Aura HD, iPad (Marvin)
|
My mistake, it's what I would have used in EditPadPro
The search was looking for _(.*)_ the brackets stores a group - everything in between the _'s The replace then done the html tags replacing the \1 with whatever was stored a good site to read through is http://www.regular-expressions.info/ |
![]() |
![]() |
![]() |
#14 |
Enquiring Mind
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 562
Karma: 42350
Join Date: Aug 2010
Location: London, UK
Device: Kindle 3 (WiFi)
|
Hi Jabby
In the regex: _(.+?)_ the brackets designate a "backreference" - essentially marking whatever string is matched by the tokens inside the brackets, so that that string can be used as part of the replacement string. You might have several of these in the search string - the first "backreference" would be referenced in the replacement string by "\1", the second by "\2", etc. In this instance, the regex is looking to match any number of characters ("." = any character, "+" = from 1 to unlimited number, "?" = as few as possible, expanding as needed) between two underscore characters. The brackets designate the text found between the underscore characters as a backreference, for use in the replace string, and since that's the first backreference specified in the regex, it will be referenced as "\1" in the replace string. The replace string: <i>\1</i> specifies a string consisting of "<i>", followed by the text matched in backreference number 1, followed by "</i>". Someone else might be able to suggest a good online tutorial, Jabby. I'm usually good at understanding technical stuff, but I have to admit I struggled to pick up regex from reading stuff online. What I've found invaluable, though, is a small program called RegexBuddy from JGSoft (the same guys who produce EditPad Pro) - it provides tools to help build regular expressions, and the way you build and view them helps you learn the syntax. It's not free, but it's not expensive either (30 Euros), so might be worth considering. |
![]() |
![]() |
![]() |
#15 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 657
Karma: 64171
Join Date: Sep 2010
Location: Kent, England, Sol 3, ZZ9 plural Z Alpha
Device: Sony PRS-300, Kobo Aura HD, iPad (Marvin)
|
@DMSmillie, the site I linked to was also done by the same team who done EditPadPro, RegexBuddy (and others)
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Increase search and replace? | user_none | Conversion | 24 | 04-08-2011 03:59 PM |
search and replace - drops blanks in replace ? | cybmole | Conversion | 10 | 03-13-2011 03:07 AM |
macro - Search and Replace | oldbwl | Workshop | 17 | 03-05-2011 01:39 PM |
Search and replace in 0.2.0 | paulpeer | Sigil | 7 | 03-13-2010 11:59 AM |
Why no search and replace? | charleski | Sigil | 10 | 11-24-2009 04:13 PM |