#1
|
|||
|
|||
Find and Replace not capturing entire line
Hi there. First a side-question: When I see "FNR" in the forum, does that mean find-and-replace?
Now the actual question: This is actually for a VBA project, but I'm testing it using the built-in FaR tool. Given this text (notice 'space space' at the beginning of some lines) EDIT!!! Forum is truncating the spaces!!!! Let me try something different. Okay... Screenshot... My setup is Code:
wildcards = true. find = "([ ]@)(<[!^l^13]{1,}>)" 'my intent is (first group = zero or more spaces)(second grp = one or more of anything but a hard or soft line return) replace = "\1+ \2" It would be nice if there were a way to specify "beginning of line/end of line," but that doesn't appear to be possible. Any thoughts on this? |
#2
|
||||
|
||||
FNR - maybe.
Use: Find = "^13[ ]@<" Replace = "^&+ " and: Find = "(^13)([! ])" Replace = "\1+ \2"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
Is your aim to find where a line wraps rather than where it is forced?
And if so, do you then want to append a "+spacespace" at the end of the line? This is going to impact the line wrap positions and be unlikely to result in what you were aiming at.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
|||
|
|||
Thanks for the replies guys! I guess it helps if I attach the document. Macropod, I can't seem to get yours to work... I tried changing "^13" into "^l", but still no luck.
Regarding line wraps vs forced; I guess I'm not sure what the definitions of these are. They are not 'wraps' in the sense that the text got to the right edge, then got wrapped automatically by Word, but they are the wrap that you get if you use "^l" in the Replace box. I guess maybe that's a "line return" and the "^13"one is a "new paragraph?" Anyway, as I look closer at my post, I think it's my first group "([ ]@)" that is the problem. I wanted it to be "zero or more spaces" but I realize now this it is "one or more spaces." In the world of regex (which I'm not an expert of!) you can tell the regex to capture a sub group if it's there, but still go ahead and capture the the other group even if it's not. I guess maybe that's not possible with Word(?) It's worth noting that I did try "([ ]{0,})" but that is apparently not valid. So my goal is to make this as flexible as possible. Most of the time, it will just be applied to a list of items that don't have any spaces at the beginning of lines. But *sometimes* there might be items that have one or two spaces. In those cases, they will be interspersed with no-beginning-space items (like in the attached). Usually they will have the 'line return' ^l at the ends. My current system is that I have a function Code:
Sub DoFindClean(FindText As String, ReplaceText As String, vWrap As String, vWild As String) 'This module gets called from the Text Cleaner code ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = FindText .Replacement.Text = ReplaceText .Forward = True .Wrap = vWrap .MatchWildcards = vWild End With Selection.Find.Execute Replace:=wdReplaceAll End Sub Among those tools is the thing I'm trying to troubleshoot. Code:
Private Sub cmdLeftPad_Click() Call DoFindClean("([!^13]{1,})", (txtPadding.Value) & "\1", 0, 1) End Sub Private Sub cmdRightPad_Click() Call DoFindClean("([!^13]{1,})", "\1" & (txtPadding.Value), 0, 1) End Sub Assess each line If first char of line = space Then first Find string. Else second find string. I actually had considered that, but I don't think that wdTextLine is an object. On a related note: It would also be excellent if I could detect the second-to-the-left character of each line. That way, if the text is as below (no spaces at line starts), it could differentiate the main ALLCAPS ones and the Word Case sub-items, and process them differently. Quote:
Last edited by Steve Kunkel; 04-21-2021 at 05:54 PM. Reason: added a thought |
#5
|
||||
|
||||
The reason it doesn't work is that your data are in table cells that terminate with end-of-cell markers, not paragraph breaks or line breaks. Your previous posts didn't even hint at that possibility. Indeed, content represented as 'READING COMPREHENSION: 66' suggested nothing more than the presence of a colon in the strings.
Use: Find = "[ A-Z]@[!^13^l]{1,}" Replace = "+ ^&" and: Find = "(+ )([ ]@<)" Replace = "\2\1"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Ugh... So sorry about that...
It *does* get applied to non-table raw text, I just forgot to convert the table to text before uploading the file. Fixed below. Anyway, I think I can probably still apply your logic: Have it capture the entire line, regardless of the spaces at the beginning. Then a second pass swaps the "bullet string" and the spaces (if they exist). Good logic :- } I will experiment more tomorrow -- Thanks again! |
#7
|
||||
|
||||
Your latest attachment has line breaks, not paragraph breaks. F/R expressions in my previous post updated accordingly.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Thanks for moving this to the correct forum. I was able to edit your above find and replace definitions to use the value from my text box. The below code works perfectly and I'm able to simply call my above posted function, which is nice.
Code:
Private Sub cmdLeftPad_Click() Call DoFindClean("([ A-Z]@[!^13^l]{1,})", (txtPadding.Value) & "\1", 0, 1) Call DoFindClean((txtPadding.Value) & "([ ]@<)", "\1" & (txtPadding.Value), 0, 1) End Sub Private Sub cmdRightPad_Click() Call DoFindClean("([ A-Z]@[!^13^l]{1,})", "\1" & (txtPadding.Value), 0, 1) End Sub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find and Replace Line Spacing | farisca | Word | 2 | 06-18-2020 04:02 PM |
Find / Replace hard Carriage Return with Line break. | GreenBoy | Word | 2 | 03-11-2018 02:32 AM |
Find/Replace in a selected cell(s), but across entire workbook | ue418 | Excel | 1 | 12-21-2017 12:32 AM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |
using find/replace on an entire directory? | mzimmers | Word | 0 | 12-13-2010 12:05 PM |