- 1 名前:デフォルトの名無しさん mailto:sage [2008/06/03(火) 22:07:28 ]
- 正規表現(Regular Expression)スレです。
質問する場合は実装言語や処理系ソフトウェア名を示しておくと話が早いです。 前スレ 正規表現 Part4 pc11.2ch.net/test/read.cgi/tech/1186030400/
- 899 名前:デフォルトの名無しさん [2009/04/16(木) 22:49:03 ]
- JavaScriptが動く HTML実験部屋
ttp://homepage2.nifty.com/tomoarai/java/exper.html
- 900 名前:デフォルトの名無しさん [2009/04/16(木) 23:01:38 ]
- <center><script> var x1, y1, x2, y2, x3, y3; var a, b, c, s, S; var S1,S2,M,N;
/*倍率*/ M=100000000; /*回数*/ N=1000; document.write('<style>td,th{font-size:10;}</style><table border=1><tr>'); document.write('<th>x1</th><th>y1</th><th>x2</th><th>y2</th><th>x3</th><th>y3</th>'); document.write('<th>底辺x高さ÷2公式<br>による面積計算結果</th>'); document.write('<th>ヘロン公式による<br>√を使った面積計算結果</th>'); document.write('<th>問題となる誤差<br>倍率'+M+'倍</th></tr>'); for(i=0;N>i;i++){ // 数値を自動代入 x1=(Math.random()*2-1)*M; y1=(Math.random()*2-1)*M; x2=(Math.random()*2-1)*M; y2=(Math.random()*2-1)*M; x3=(Math.random()*2-1)*M; y3=(Math.random()*2-1)*M; a=Math.abs(x1-x2)*Math.abs(y1-y2)/2; // 底辺x高さ÷2公式による面積の計算 b=Math.abs(x2-x3)*Math.abs(y2-y3)/2; c=Math.abs(x3-x1)*Math.abs(y3-y1)/2; s=(Math.max(Math.max(x1,x2),x3)-Math.min(Math.min(x1,x2),x3)) s*=(Math.max(Math.max(y1,y2),y3)-Math.min(Math.min(y1,y2),y3)); S=s-a-b-c; S1=S; a = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); // ヘロン公式による√を使った面積の計算 b = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2)); c = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); s = (a + b + c) / 2.0; S = Math.sqrt(s * (s - a) * (s - b) * (s - c)); S2=S; document.write('<tr><td> '+x1+'</td><td> '+y1+'</td>'); // 計算結果と誤差を表示 document.write('<td> '+x2+'</td><td> '+y2+'</td><td> '+x3+'</td><td> '+y3+'</td>'); document.write('<td> '+S1+'</td><td> '+S2+'</td><td> '+(S1-S2)+'</td></tr>'); }document.write('</table>'); </script>
- 901 名前:デフォルトの名無しさん [2009/04/16(木) 23:02:20 ]
- >>888 >>900 ⇒ >>899
- 902 名前:デフォルトの名無しさん [2009/04/16(木) 23:13:33 ]
- /*倍率*/ M=1000; // 変更
- 903 名前:デフォルトの名無しさん mailto:sage [2009/04/16(木) 23:20:59 ]
- 言語Perlです。
あるCのソースから構造体(typedefも)を抽出したいのですが、 この例のようなstructやunionを含んでるstructを マッチさせるにはどう書けばいいんですか? struct A{ unsigned short a; unsigned long b char* c; struct{ long e[8]; long f; }d; };
- 904 名前:デフォルトの名無しさん [2009/04/16(木) 23:24:21 ]
- ↑一旦文字変換してから
matchさせると良い
- 905 名前:デフォルトの名無しさん [2009/04/16(木) 23:29:33 ]
- -21761253.57183114 8544355.772346469 61664545.39556769 62137654.31616132 79934094.82325418 66218011.1129441 659766221291321.5 319359195447661.56 340407025843659.94
- 906 名前:デフォルトの名無しさん [2009/04/16(木) 23:47:31 ]
- たとえば?
data=list*->A.c; if(data.match(/.*/)){ }
- 907 名前:デフォルトの名無しさん [2009/04/16(木) 23:48:55 ]
- x=(79934094.82325418)-(-21761253.57183114);
y=(66218011.1129441)-(8544355.772346469);
- 908 名前:デフォルトの名無しさん mailto:sage [2009/04/17(金) 00:15:56 ]
- >>903
>この例のようなstructやunionを含んでるstructを 一般に、任意にネストできる構造は正規表現で照合できない (実は、「Perlの」正規表現ではできてしまうけど、ごちゃごちゃしたものになるので オススメしない)。正確に抽出したいならCのパーザを自前で書くか、どっかからCのパーザ 拾ってくるのが良い
- 909 名前:デフォルトの名無しさん [2009/04/17(金) 00:22:26 ]
- 簡単な検証方法
<center><script> var x1, y1, x2, y2, x3, y3; var a, b, c, s, S; var S1,S2,M,N; /*可変*/ N=1000; document.write('<style>td,th{font-size:10;} </style><table border=1><tr>'); document.write('<th>x1 </th><th>y1 </th><th>x2 </th><th>y2 </th><th>x3 </th><th>y3 </th>'); document.write('<th>底辺x高さ÷2公式<br>による面積計算結果 </th>'); document.write('<th>ヘロン公式による<br>√を使った面積計算結果 </th>'); document.write('<th>問題となる誤差<br>倍率'+M+'倍 </th> </tr>'); for(i=-N;N>=i;i++){ // 数値を自動代入 x1=0; y1=i; x2=-1; y2=0; x3=1; y3=0; a=Math.abs(x1-x2)*Math.abs(y1-y2); // 底辺x高さ÷2公式による面積の計算 b=Math.abs(x2-x3)*Math.abs(y2-y3); c=Math.abs(x3-x1)*Math.abs(y3-y1); s=(Math.max(Math.max(x1,x2),x3)-Math.min(Math.min(x1,x2),x3)) s*=(Math.max(Math.max(y1,y2),y3)-Math.min(Math.min(y1,y2),y3)); S=s-(a+b+c)/2; S1=S; a = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); // ヘロン公式による√を使った面積の計算 b = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2)); c = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); s = (a + b + c) / 2.0; S = Math.sqrt(s * (s - a) * (s - b) * (s - c)); S2=S; document.write('<tr><td> '+x1+' </td><td> '+y1+' </td>'); // 計算結果と誤差を表示 document.write('<td> '+x2+' </td><td> '+y2+' </td><td> '+x3+' </td><td> '+y3+' </td>'); document.write('<td> '+S1+' </td><td> '+S2+' </td><td> '+(S1-S2)+' </td> </tr>'); }document.write(' </table>'); </script>
- 910 名前:デフォルトの名無しさん [2009/04/17(金) 00:28:10 ]
- 誤差を検証してみた
その結果√を使用すると 誤差が大きいと判断される >>909 ⇒ >>900 ⇒ >>888 JavaScriptが動く HTML実験部屋 ttp://homepage2.nifty.com/tomoarai/java/exper.html
- 911 名前:デフォルトの名無しさん [2009/04/17(金) 00:44:36 ]
- perl5.8.8を使用しています。
<td>タグの中身を取得したいのですが、ネストしている場合は、 内側のタグのみを対象としたいです。 そこで否定先読みで<td>と</td>の間の文字列でかつ、"<td>"に続かない ものにマッチするという正規表現を書こうとしています。 現状では以下のような感じです。 $_ =<<DATA; <td>データ1</td> <td> <tr> <td>データ2</td> </tr> </td> DATA # 内側のテーブルタグのみにマッチさせたい @res = /<td>(?![\s\w<>]+?<td>)[\s\S]+?<\/td>/gm; # こっちはOK #@res = /<td>(?![\s\S]+?<td>)[\s\S]+?<\/td>/gm; # NG 分からないのはNGと書いた方が上手く動かない理由です。 現象としては、否定先読み条件の中の最短マッチが適用されていないように思います。 上の例ですと、データ2のみが取得されてしまいます。 修正方法を教えていただけないでしょうか。
- 912 名前:デフォルトの名無しさん [2009/04/17(金) 02:14:58 ]
- ↑
[\s\S]って . と同じ?
- 913 名前:デフォルトの名無しさん [2009/04/17(金) 02:36:13 ]
- >>911 ⇒ >>910 サンプル
<center><script> var x1, y1, x2, y2, x3, y3; var a, b, c, s, S; var S1,S2,M,N; /*可変*/ N=1000; P=''; P+=('<style>td,th{font-size:10;} </style><table border=1><tr>'); P+=('<th>x1 </th><th>y1 </th><th>x2 </th><th>y2 </th><th>x3 </th><th>y3 </th>'); P+=('<th>底辺x高さ÷2公式<br>による面積計算結果 </th>'); P+=('<th>ヘロン公式による<br>√を使った面積計算結果 </th>'); P+=('<th>問題となる誤差<br>倍率'+M+'倍 </th> </tr>'); for(i=-N;N>=i;i++){ // 数値を自動代入 x1=0; y1=i; x2=-1; y2=0; x3=1; y3=0; a=Math.abs(x1-x2)*Math.abs(y1-y2); // 底辺x高さ÷2公式による面積の計算 b=Math.abs(x2-x3)*Math.abs(y2-y3); c=Math.abs(x3-x1)*Math.abs(y3-y1); s=(Math.max(Math.max(x1,x2),x3)-Math.min(Math.min(x1,x2),x3)) s*=(Math.max(Math.max(y1,y2),y3)-Math.min(Math.min(y1,y2),y3)); S=s-(a+b+c)/2; S1=S; a = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); // ヘロン公式による√を使った面積の計算 b = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2)); c = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); s = (a + b + c) / 2.0; S = Math.sqrt(s * (s - a) * (s - b) * (s - c)); S2=S; P+=('<tr><td> '+x1+' </td><td> '+y1+' </td>'); // 計算結果と誤差を表示 P+=('<td> '+x2+' </td><td> '+y2+' </td><td> '+x3+' </td><td> '+y3+' </td>'); P+=('<td> '+S1+' </td><td> '+S2+' </td><td> '+(S1-S2)+' </td></tr>');}P+=('</table>'); data=P; data=data.replace(/(<td>)([^<>]*)(<\/td>)/g,'$1 消去$3'); document.write(data); </script>
- 914 名前:デフォルトの名無しさん [2009/04/17(金) 02:37:52 ]
- >>911
これを参考に・・・ data=data.replace(/(<td>)([^<>]*)(<\/td>)/g,'$1消去$3');
- 915 名前:デフォルトの名無しさん [2009/04/17(金) 02:51:50 ]
- >>895
せっかく>>876を行列式で書くなら 3D空間で同様に書いてみたら? つまり宇宙的規模と言う事です。 (x1,y1,z1) (x2,y2,z2) (x3,y3,z3)
- 916 名前:デフォルトの名無しさん [2009/04/17(金) 02:54:13 ]
- ↑2Dサンプルは>>910参照
- 917 名前:デフォルトの名無しさん mailto:sage [2009/04/17(金) 03:08:46 ]
- >>912
同じといえば同じ。 ただし、. と違って常に改行にもマッチする。 否定形で使うと何ともマッチすることのないものになる。
|

|