文字列の改行を削除 3回目
文字列の改行を徹底的に削除←なんてやってたけど、どうもうまくいかない。
仕方ないので一文字づつ処理していく事にした。
Dim temContents As String = String.Empty Dim newCont As String Dim temCont As String = String.Empty For Each tItem As TextArtItem In IllDoc.TextArtItems newCont = String.Empty With tItem temContents = Replace(Trim(.Contents), " ", String.Empty) 'スペースを削除 If (.TextRange.TextLines.Count >= 2) AndAlso (.Contents.Count < 8) Then '不要なテキストを選別し除外 For itemcount As Integer = 1 To Len(temContents) '1文字づつ繰り返す temCont = Mid(temContents, itemcount, 1) '1文字取得 If Not (Asc(temCont) = 3 Or Asc(temCont) = 13) Then '[3]-ETX(テキスト終了),[13]-CR(復帰)なので除外してテキストを作成 newCont = newCont + temCont End If Next temContents = newCont '改行を除いた文字を代入 End If End With Next
上記は不要な文字を除くやり方をしたが、Regexを使って必要な文字だけを拾っていく方法もある。
If Not (Asc(temCont) = 3 Or Asc(temCont) = 13) Then '[3]-ETX(テキスト終了),[13]-CR(復帰)なので除外してテキストを作成 newCont = newCont + temCont End If
この部分を
If Regex.IsMatch(temCont, "^([A-z]|[A-z]|[ア-ン]|[0-9]|[ア-ン]|゙|゚)$") Then newCont = newCont + temCont End If
こうすれば正規表現で一致したテキストだけ取得できる。
ここまで来たら、もうちょっとキレイに整形したいなぁなんて欲が出てきた。
で、全角・半角が混在するテキストを「英数は半角・カナは全角」にしてみようと思う。
If Regex.IsMatch(temCont, "^([A-z]|[A-z]|[0-9]|[0-9])$") Then newCont = newCont + StrConv(temCont, VbStrConv.Narrow) ElseIf Regex.IsMatch(temCont, "^([ア-ン]|[ア-ン]|゙|゚)$") Then newCont = newCont + StrConv(temCont, VbStrConv.Wide) End If
するとこんな感じか。しかしこれでは半角の「ド」が「ト゛」になってしまう。これじゃダメだ。
という訳でテキストを整形するファンクションを新設。
半角濁点の対策として、最初に全て全角に変換してから処理する事に。
とりあえず、コレで良いでしょう。
Imports System.Text.RegularExpressions Function textSmart(ByVal Str As String) '処理したくないアイテムは事前に判別しておく Dim newContents As String = String.Empty Dim temContents As String = String.Empty Str = StrConv(Str, VbStrConv.Wide) '全て全角にする For ItemCount As Integer = 1 To Len(Str) '1文字づつ繰り返す temContents = Mid(Str, ItemCount, 1) '1文字取得 If Regex.IsMatch(temContents, "^([A-z]|[0-9]|―|−|‐)$") Then '英数記号は半角に newContents = newContents + StrConv(temContents, VbStrConv.Narrow) 'ElseIf Regex.IsMatch(temContents, "^([ア-ン])$") Then 'カナは全角のまま Else 'そうでなければ全角のまま newContents = newContents + temContents End If Next Return newContents End Function