Home > Software engineering > Use regular expressions to replace the WORD tail Spaces appear the strange phenomenon
Use regular expressions to replace the WORD tail Spaces appear the strange phenomenon
Time:11-16
Consult everybody warrior experts! Use regular expressions to replace tail segment space, if a paragraph, followed by form is replaced is the text of paragraph will run to the table, in addition to other conditions are normal, mystery, please supervise your teacher for advice,
Sub delete paragraph tail Spaces () Dim I As com.lowagie.text.paragraph, mt, oRang As Range, n % m % The Set regx=CreateObject (" vbscript. Regexp ") With regx The Global=True: IgnoreCase=False: MultiLine=True . The Pattern="+ (\ r \ s" 'delete the blank space at the end of the paragraph, if there is no \ r, will as a space together with a carriage return, For Each I In ActiveDocument. Paragraphs 'In Each paragraph traversal For Each mt In the Execute (i.R ange. Text) 'traverse the matched Each Text M=mt. FirstIndex: n=mt. Length - 1 'for the starting position of the (starting from zero), the Length of the Set oRang=ActiveDocument. Range (i.R ange. Start + m, i.R ange. Start + m + n) 'locating to the text of the starting position in the whole article, end position I.R ange. Text=Replace (i.R ange. The Text, "$1") Next Next End With End Sub
Before running the document
After running the document
CodePudding user response:
delete Sub paragraph tail Spaces () Dim I As com.lowagie.text.paragraph, mt, oRang As Range, n % m %, mRange As Range The Set regx=CreateObject (" vbscript. Regexp ") With regx The Global=True: IgnoreCase=False: MultiLine=True . The Pattern="\ s + $" 'delete the blank space at the end of the paragraph, if there is no \ r, will as a space together with a carriage return, For Each I In ActiveDocument. Paragraphs 'In Each paragraph traversal The Set mRange=ActiveDocument. Range (i.R ange. Start, i.R ange. End - 1) For Each mt. In the Execute (mRange. Text) 'traverse the matched Each Text M=mt. FirstIndex: n=mt. Length of 'looking for the starting position of the (starting from zero), the Length of the Set oRang=ActiveDocument. Range (mRange. Start + m, mRange. Start + m + n) 'locating to the text of the starting position in the whole article, end position ORang. Text=. Replace (oRang. Text, "") Next Next End With End Sub
CodePudding user response:
I can you say the strange phenomenon of show here. Don't use regular expressions, but directly to the original Text assigned to the Text, also will have the same question. I tried for a long time, can only find a basic instead of the regular expression substitution method, you can try:
delete Sub paragraph tail Spaces () Dim objFind As Find The Set objFind=ActiveDocument. Content. The Find ObjFind. Execute FindText:="^ w ^ 13", ReplaceWith:="^ 13", Replace:=wdReplaceAll End Sub
This code is executed after also have a question, is to form a line above can't remove the Spaces, form inside can't remove the Spaces. Other places probably no problem.
CodePudding user response:
But, why the WORD "interface to find replacement" space won't appear this kind of strange phenomenon, I am a novice, here put forward this problem, main is to want to the great god help to solve this problem, use VBA regular expression, how can I avoid this strange phenomenon