The nice thing about searching first is I always find useful stuff I didn't know I needed until I found it by accident. I'd like to thank the people that take time to answer questions in this forum. I'm only learning regex but even my pitiful beginnings have made a night and day difference for me.
I manage an area in a corp where vendors do a great deal of work converting messy text and various software outputs into formats that can be machine parsed, so as you can imagine it's just a lifesaver for that stuff. I can't believe I never made more effort for this before! Now the vendors are upset because I'm saying "Hey I just did in 47 minutes, moving slowly, what it took you 3 weeks and $500-1200 to get back to me. How about you learn some decent s/r/regex so you can be competitive with cheap outside contractors I'd like to hire for 20 bucks an hour to do this instead?"
I'm just shocked at how much can be automated on the fly with this stuff once you learn even a few simple things! I think I'm really going to be able to reduce costs in my area, one of my directives from above, if I can work out stuff and share it with the people working for me. (Who really ought to learn this themselves, but cost/time means more to me than demanding that.)
It's like another language in a way, really cool.
To get to the point... I've been using a little s/r regex (posix) to grab "PTS: 1 " from any line and convert it to <value name="points">\1</points>\n (search: PTS:([^DIF]*) where DIF is text that comes after that on the line).
The problem is it captures and puts in the spaces. Now I grant I can search and replace spaces from the front and then the back of that tag, but I do this stuff on tons of tags, so what I really want is just a way to say "capture everything from here to there but when you paste it in, trim any spaces on either side of the captured content." If it helps, I can make sure the spaces are only ONE each on each side first, before doing this.
I searched in textpad and on 'trim' in this forum but I didn't find anything. Thanks for any assistance.
Best,
PJ
Trim function within a replace
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
That is not at all clear. You haven't provided sample text before and after the replacement. And I don't understand the [^DIF] bit.
Is this what you need?
Use "Posix" regular expression syntax:
Is this what you need?
Use "Posix" regular expression syntax:
Search | Replace... (<F8>):Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
Find what: PTS: *(.*[^ ]) *
Replace with: <value name="points">\1</points>\n
[X] Regular expression
Replace All
So sorry, you're right.
Text might come in looking like this:
and needs to end up looking like:
(note that I'll be doing this same thing with the other values on this line, not erasing them.)
Your selection doesn't exclude the following text I need to although it does seem to trim spaces at the left and right. Unfortunately when I use the normal thing I would to 'stop the search' (that is where 'DIF' comes in, I'm stopping the selection at that point) it doesn't work now with the diff code so I'm not sure how to do it.
Many many thanks.
PJ
Text might come in looking like this:
Code: Select all
1. ANS: T PTS: 1 DIF: Easy REF : 131Code: Select all
<value name="points">1</points>Your selection doesn't exclude the following text I need to although it does seem to trim spaces at the left and right. Unfortunately when I use the normal thing I would to 'stop the search' (that is where 'DIF' comes in, I'm stopping the selection at that point) it doesn't work now with the diff code so I'm not sure how to do it.
Many many thanks.
PJ
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
That solved a problem I didn't list so thank you, very helpful!
But in this case I'm not trying to clear the whole line, I am trying to replace one value:pair on that line. I will need to replace (separately) all the other value pairs with their own tags, too, so I can't just delete them. (In previous post I said, "(note that I'll be doing this same thing with the other values on this line, not erasing them.)")
So I was only trying to "focus in on the PTS value" to get from
I didn't state the larger picture because the value:pairs available vary and the ANS is dealt with slightly differently (still search/replace) and I didn't want to cause confusion. But if it helps, in the end after applying this logic to whatever value:pairs might exist (it varies), for the line above I'm going to end up with something like:
Best,
PJ
But in this case I'm not trying to clear the whole line, I am trying to replace one value:pair on that line. I will need to replace (separately) all the other value pairs with their own tags, too, so I can't just delete them. (In previous post I said, "(note that I'll be doing this same thing with the other values on this line, not erasing them.)")
So I was only trying to "focus in on the PTS value" to get from
Code: Select all
1. ANS: T PTS: 1 DIF: Easy REF : 131
to
<value name="points">1</value>Code: Select all
1.
T
Correct.
F
Incorrect.
<value name="points">1</value>
<value name="skilllevel">1</value>
<value name="sourcereference">131</value>PJ
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
It's difficult to provide help in the face of changing requirements.
Where do the items Correct., F and Incorrect. come from? It may be easier to insert these and to change Easy to 1 before splitting the line.
This splits the line as required, but doesn't add or translate any items.
Where do the items Correct., F and Incorrect. come from? It may be easier to insert these and to change Easy to 1 before splitting the line.
This splits the line as required, but doesn't add or translate any items.
Find what: ([^.])\. +ANS: *([^ ]+) +PTS: *([^ ]+) +DIF: *([^ ]+) +REF : *([^ ]+)
Replace with: \1.\n\2\n<value name="points">\3</value>\n<value name="skilllevel">\4</value>\n<value name="sourcereference">\5</value>\n
[X] Regular expression
Replace All
In a way I didn't expect this totally solved the problem.
The real problem (aside from my being a little dense...) is that since these value:pairs vary, in what's included, where it is on the line, etc. sometimes it works great like you showed, and can be done that way.
I had figured out how to deal with something if it was alone on a line or at the end of a line, but I was having trouble figuring out how to find something in the middle of a line and then capture everything up until a certain string. Of course now it seems to obvious and I can just say it that way...
Your script made me realize that if for some reason these lines varied in the content (which they sometimes do for me, part of the iffy-ness I was having), one should just move everything onto its own line (which your script helped me do). Then it's easier to deal with whatever is there without having to worry about what's to the right of them for example.
Thanks again for the time you invest in helping people figure this stuff out. Everything I learn here is a tool that expands a lot as soon as I know it since I see how it can be applied in other ways. I have a lot to learn still but it's kinda fun and it helps IMMENSELY to have an expert to ask questions of.
PJ
The real problem (aside from my being a little dense...) is that since these value:pairs vary, in what's included, where it is on the line, etc. sometimes it works great like you showed, and can be done that way.
I had figured out how to deal with something if it was alone on a line or at the end of a line, but I was having trouble figuring out how to find something in the middle of a line and then capture everything up until a certain string. Of course now it seems to obvious and I can just say it that way...
Your script made me realize that if for some reason these lines varied in the content (which they sometimes do for me, part of the iffy-ness I was having), one should just move everything onto its own line (which your script helped me do). Then it's easier to deal with whatever is there without having to worry about what's to the right of them for example.
Thanks again for the time you invest in helping people figure this stuff out. Everything I learn here is a tool that expands a lot as soon as I know it since I see how it can be applied in other ways. I have a lot to learn still but it's kinda fun and it helps IMMENSELY to have an expert to ask questions of.
PJ