regex S&R - seems like it ought to be simple, but...!

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
redcairo
Posts: 39
Joined: Fri May 06, 2011 6:34 am

regex S&R - seems like it ought to be simple, but...!

Post by redcairo »

I see I put off learning about regular expressions too long, since now I desperately need to write one for a big bulk of stuff and am starting from scratch. I read textpad's help and I read a couple of websites with sort of tutorials. It does seem pretty straightforward albeit complex with all the options and little-logical catches easy to miss. But when I came up with something I thought would work, if I try to use it in textpad search (with or without POSIX spec'd), as regular expression checked, it just tells me it can't find it-- as if, perhaps, it's looking for the literal characters of my regex. Or, I just completely sucked at choosing a regex I suppose.

I searched this forum and read several threads but didn't get an answer (though I learned some other things!).

The content I am trying to deal with follows. This is the 'literal' content. The 'escaped tags' and 'See ' at the front are always the same, but everything in the middle varies (the value:pair values).

Code: Select all

See <clebook id="rec"><ebook-link eISBN="9780538475327" contextualized="true" chapterName="Effective and Ethical Communication at Work" sectionName="Communication Skills and You">pages 4-5.</ebook-link></clebook>
I figured I should do a S&R on the whole product first to get rid of all the escaped HTML in the middle of the XML. Of course this is a problem since it might un-escape stuff that needs escaping (I am a bit new to what I'm dealing with here so I'm not sure). But, trying to simplify it for myself, I thought I would start easy: change the escaped html to tags, forget about the 'See ' for a moment, and just try to figure out how to 'remove' (literally) the full tag spec. So, that would leave me with:

Code: Select all

<clebook id="rec"><ebook-link eISBN="9780538475327" contextualized="true" chapterName="Effective and Ethical Communication at Work" sectionName="Communication Skills and You">pages 4-5.</ebook-link></clebook>
After reading awhile I concluded that since I had an opening and closing tag, surely the example scripts that would "find any tag and everything in it" would work. I tried these two variants in the 'search' field in textpad:

Code: Select all

<clebook\b[^>]*>(.*?)</clebook>
and
<clebook id="rec"\b[^>]*>(.*?)</clebook>
It just tells me it can't find it.

In a perfect world, I'd work out a regex that used the escaped code these files are coming to me with, and get rid of the 'See ' that leads these too, as that's what really needs doing. But I can't even figure out the tag part. I am really dense apparently!

I think there must be something I am fundamentally misunderstanding about how this works. Any pointers would be greatly appreciated.

Best,
PJ
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

TextPad's regex recogniser is old and weak, and many regexes that work with modern tools do not work in TextPad. In particular, TextPad's regex recogniser doesn't support non-greedy repetition operators (such as *?), and \b does not represent a word boundary.

To match the clebook elements in your text, use "Posix" regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
and try these:
<clebook\>[^>]*>(.*)</clebook>
<clebook id="rec"[^>]*>(.*)</clebook>

As for changing your text, I do not understand what you are trying to do. Please explain more clearly, with examples of the text as it is and as you want it.
redcairo
Posts: 39
Joined: Fri May 06, 2011 6:34 am

Post by redcairo »

Your suggestion worked perfectly.

(I ended up doing the text part manually as it was different for every entry and unpredictable, even with pattern... couldn't be separated by script from the rest of the content.) Thank you SO much for your assistance.

The rest of the work I'm doing, is probably a no-brainer but I think something about S&R vs. Regex is messing me up. Basically I just need to say, here's a string, and this value in the middle will vary in alphanum chars and digits (qty of them), strip it all. Such as:

<cl:para identifier="WITZGC201037782" id-sequence="799">

I just want to strip it to :

<cl:para identifier="" id-sequence="799">

(and do the same thing, separately is fine, with the id-sequence, such as:

<cl:para identifier="" id-sequence="">

)

Because then I could S&R what is left into a <p> tag. Unless there is actually a way to do both of those (that whole line) into a P tag (I'm not sure what's possible, I'm still new to this).

My regex of [a-zA-Z0-9] nicely wipes out all alphanum chars but when I try to put that "inside" a string (such as identifier="[a-zA-Z0-9]" ) it just doesn't "find" anything. I tried some other things (\b and other chars) I found on a tutorial website, but I don't think it's reading those the way expected. (I have posix on and regex checked.)

Any ideas?

Many thanks,
PJ
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

[a-z0-9] matches a single character. You need
[a-z0-9]* to match any number (possibly zero) of them, or
[a-z0-9]+ to match any non-zero number of them.

You don't need to include both upper and lower case unless you've selected Match case.
redcairo
Posts: 39
Joined: Fri May 06, 2011 6:34 am

Post by redcairo »

I got the "replace chars" part, the part that I'm stumped on, is how to make this happen "only in the context of a certain larger string."

So if I have

Code: Select all

identifier="HDVLJQ445247977"
When I try to search/replace in textpad, if I use just the bracketed stuff it wipes out ALL alphanum in the whole doc, but if I put the

Code: Select all

identifier="[regexhere]"
in, it just says it can't find it. How do I tell it only to look for that stuff between those doublequotes?

PJ[/code]
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

I don't understand. Does this do part of what you need?
Find what: <cl:para identifier="[a-z0-9]*" id-sequence="[a-z0-9]*">
Replace with: <cl:para identifier="" id-sequence="">

[X] Regular expression

Replace All
Give examples of your text as it is and your text as you'd like it to be.
redcairo
Posts: 39
Joined: Fri May 06, 2011 6:34 am

Post by redcairo »

Apparently the asterisk is what I was missing. It "finds" it now, with that code. Thank you so incredibly much!!

I only have one question left I hope (should I make a new thread)? and this is one I haven't been able to figure out at ALL with tutorial sites. (I might add that reading posts here helped me figure out several things I haven't had to bug anybody about as a result.)

There's got to be a way to sort of... well I don't know how to ask for it. Lemme think... ok, there is a value between tags which varies. I need to replace the tagset but I need the value to stay the same as it was. Like:

Code: Select all

<cl:style styles="italic">high</cl:style>
or:

Code: Select all

<cl:style styles="bold" color="1">5 points:</cl:style>
now the values 'between' the tagsets are going to vary of course. I want to replace the tags with

Code: Select all

<em>whatever</em> 
and 
<strong>whatever</strong>
The S&R and the regex to vary things like what the values are inside the tags themselves, I think I have that.

But how do I 'replace' with 'whatever the value was inside'? Maybe I'm thinking of it wrong, am I not really replacing it but only replacing around it?

PJ
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Capture the text you want to keep with parentheses and insert it into the replacement with \1, \2, etc.

Use "Posix" regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Then
Find what: <cl:style styles="italic">([^<]*)</cl:style>
Replace with: <em>\1</em>

[X] Regular expression

Replace All
Find what: <cl:style styles="bold" color="1">([^<]*)</cl:style>
Replace with: <strong>\1</strong>

[X] Regular expression

Replace All
Post Reply