2017-10-22 1 views
0

私はAnkiフラッシュカードの多項選択問題の.docxを解析しようとしています。いくつかの多項選択問題のJavaScriptを使った正規表現

私はdocxから 'Questions'と 'Answers'のマッチグループを作りたいと思っています。

質問をしようとするうちに、私が望むようにそれぞれの正規表現を一致させることはできません。

質問の例:文書全体を解析するとき、私は/(\d.)[^\d][^ANS$]+/gmを使用していますが、それは2番目と3番目の質問を飛ばし続け、そしてより

1. Hormones and other signal molecules bind with ____ affinities to their receptors and are produced at concentrations ____ their KD values. 
a. 
low; far above 
b. 
moderate; far above 
c. 
moderate; equivalent to 
d. 
high; far below 
e. 
very high; equivalent to 


ANS: E  

    2. Steroid hormones, such as glucocorticoids, effect their action by: 
a. 
binding to a plasma membrane receptor, which stimulates a signal transduction pathway within the cell 
b. 
binding to a plasma membrane receptor, which stimulates the receptor to enter the cell 
c. 
entering into the cell and affecting the production of secondary messengers 
d. 
entering into the cell and then acting as transcription regulators 
e. 
both a and d are correct 


ANS: E  

    3. All are unifying features of polypeptide hormones EXCEPT that they are: 
a. 
originally synthesized with signal sequences. 
b. 
synthesized as inactive preprohormones. 
c. 
activated from preprohormones to hormones by phosphorylation. 
d. 
may produce several different peptide hormones with suitable processing. 
e. 
all are true. 


ANS: C  

    4. Each of the following statements is true EXCEPT: 
a. 
epinephrine is an amino acid derivative 
b. 
steroid hormones can enter cells and regulate transcription 
c. 
insulin is a polypeptide hormone 
d. 
progesterone is a polypeptide hormone 
e. 
all of the above are true 


ANS: D  

    5. The acrosome reaction, involving ion channel induced release of acrosomal enzymes used by sperm to attack the egg, is induced by: 
a. 
estrogen. 
b. 
testosterone. 
c. 
dihydrotestosterone (DHT). 
d. 
progesterone. 
e. 
cortisol. 


ANS: D  

アドバイスをいただければ幸いです。

+0

':それはANS

デモ以外のものによって中断されません

(\d\.)[\s\S]*?(?=ANS) 

を'。 – melpomene

+0

あなたは何を得たいのかを明確にすることができますか、少なくともあなたのサンプルが与えられた出力はどれくらいでしょうか? – PJProudhon

答えて

0

また、簡単に解決を試みることができる:[^ \ D] `良くは\ D 'のように書かhttps://regex101.com/r/Qnwl8b/2/

+0

これは実際には最高のものでしたが、一度変更してからは、より多くの質問に対応できました:/(¥d{1,2}\.)[¥s\S]*???????????????????????????????????????????????? – mjabraham

+0

@mjabraham最高の、私のパフォーマンスを測定するだけです。そしてそれは答えに一致しません。 –

+1

@Ibrahim長いテキスト(または任意の長さのテキスト)を解析するときは、先読みで遅延ドットマッチングを使用しないでください。 '[\ s \ S] *?(?= ANS)'は '' \\ \\\\ ''パターンをスキップし、 'ANS'をチェックし、' ANS'が見つからない場合、 [\ s \ S] *?パターンは展開されています(バックトラックとは逆の動作です)。この操作は、文字列内のすべての単一の場所で行います。これらのパターンは、パフォーマンスを向上させるために展開する必要があります。 –

1

(\d.)[^\d][^ANS$]+パターンは桁(\d)はANS$以外の1つのまたはそれ以上の文字が続く、その後、数字以外の文字([^\d])と、続いて、次いで、任意のCHAR(.)と、続いて一致(は無効文字クラスで、$は特別な意味を失い、シーケンスではなくクラスからの1つの文字に一致します)。 m修飾子は^が開始と一致しますので、ライン(の開始 - 正規表現を修正するため

、あなたはregex demo

詳細

  • ^を参照してください

    /^\s*(\d+\..*(?:\r?\n(?!\s*ANS:).*)*)\r?\n\s*(ANS:.*)/gm 
    

    を使用することができます文字列全体ではなく行の数)

  • 個の
  • \s*から0以上の空白
  • ( - グループ1つのスタート:
    • \d+から1桁以上
    • \. - ドット
    • .* - ライン
    • (?:\r?\n(?!\s*ANS:).*)*の残り -
      • 01の0回以上の連続した出現- CRLFまたはLF改行...
      • (?!\s*ANS:) - 0+空白(\s*)と続いていないが ANS:サブ
      • .*(?!...)は そのパターンは現在位置のすぐ右側に見つかった場合、一致に失敗負先読みである)と、続いて - グループ1の端
      • から
  • )ラインの残りの部分0 - 改行
  • \s* - 0+空白
  • (ANS:.*) - グループ2 ANS:と行の残りの部分をキャプチャします。

JSのデモ:

var rx = /^\s*(\d+\..*(?:\r?\n(?!\s*ANS:).*)*)\r?\n\s*(ANS:.*)/gm; 
 
var s = "1. Hormones and other signal molecules bind with ____ affinities to their receptors and are produced at concentrations ____ their KD values.\r\na.\r\nlow; far above\r\nb.\r\nmoderate; far above\r\nc.\r\nmoderate; equivalent to\r\nd.\r\nhigh; far below\r\ne.\r\nvery high; equivalent to\r\n\r\n\r\nANS: E \r\n\r\n 2. Steroid hormones, such as glucocorticoids, effect their action by:\r\na.\r\nbinding to a plasma membrane receptor, which stimulates a signal transduction pathway within the cell\r\nb.\r\nbinding to a plasma membrane receptor, which stimulates the receptor to enter the cell\r\nc.\r\nentering into the cell and affecting the production of secondary messengers\r\nd.\r\nentering into the cell and then acting as transcription regulators\r\ne.\r\nboth a and d are correct\r\n\r\n\r\nANS: E \r\n\r\n 3. All are unifying features of polypeptide hormones EXCEPT that they are:\r\na.\r\noriginally synthesized with signal sequences.\r\nb.\r\nsynthesized as inactive preprohormones.\r\nc.\r\nactivated from preprohormones to hormones by phosphorylation.\r\nd.\r\nmay produce several different peptide hormones with suitable processing.\r\ne.\r\nall are true.\r\n\r\n\r\nANS: C \r\n\r\n 4. Each of the following statements is true EXCEPT:\r\na.\r\nepinephrine is an amino acid derivative\r\nb.\r\nsteroid hormones can enter cells and regulate transcription\r\nc.\r\ninsulin is a polypeptide hormone\r\nd.\r\nprogesterone is a polypeptide hormone\r\ne.\r\nall of the above are true\r\n\r\n\r\nANS: D \r\n\r\n 5. The acrosome reaction, involving ion channel induced release of acrosomal enzymes used by sperm to attack the egg, is induced by:\r\na.\r\nestrogen.\r\nb.\r\ntestosterone.\r\nc.\r\ndihydrotestosterone (DHT).\r\nd.\r\nprogesterone.\r\ne.\r\ncortisol.\r\n\r\n\r\nANS: D " 
 
var m; 
 
var qst=[], ans=[]; 
 
while (m = rx.exec(s)) { 
 
    qst.push(m[1].trim()); 
 
    ans.push(m[2].trim()); 
 
} 
 
document.body.innerHTML += "<pre>" + JSON.stringify(qst, 0, 4) + "</pre>"; 
 
document.body.innerHTML += "<pre>" + JSON.stringify(ans, 0, 4) + "</pre>";

+0

ここで質問をフォローアップしてください:https://stackoverflow.com/questions/46878545/using-regex-with-javascript-on-answers-to-multiple-choice-questions – mjabraham

+0

質問と回答の両方をキャプチャするバージョンは['' *)*)\ r?\ n \ s *(ANS:。*) '](\ https://regex101.com/r/4CzB4k/2)、[** the JSFiddle **](https://jsfiddle.net/cn26ctrn/)をご覧ください。 –

1

あなただけの質問を選択したい場合、あなたは、単に使用することができます。

/\d.+/gm 

をあなたがそれぞれの質問を選択したい場合は、その答えは、あなたが使用することができます:

/\d\D+(?=ANS)/gm 

これは、質問と回答に数値が含まれていないことを前提としています。