簡體   English   中英

如何使用正則表達式識別不帶引號的字段名稱?

[英]How to recognize a field name without quotation marks with a regex?

我編寫了一個解析器來識別這種類型的字符串: TERM: MATCH_TERM

我最初只允許在單引號或雙引號( 'TERM'"TERM" )之間寫TERM部分。 我現在想允許在不帶引號的情況下編寫TERM部分。

我用於識別被引號包圍的TERM部分的正則表達式效果很好,如下所示:

const StringDoubleQuote = createToken({ name: "StringDoubleQuote", pattern: /"[^"\\]*(?:\\.[^"\\]*)*"/ });
const StringSimpleQuote = createToken({ name: "StringSimpleQuote", pattern: /'[^'\\]*(?:\\.[^'\\]*)*'/ });

對於這些正則表達式,我不需要指定:字符作為標記結尾的標記,因為引號已經具有這種用法。

為了使TERM部分可以不用引號括起來,我使用了以下正則表達式:

const StringWithoutQuote = createToken({ name: "StringWithoutQuote", pattern: /[\w!@#\$%\^&.-]+/ });

為了定義使用chevrotain創建的解析器的詞法分析器部分,我編寫了以下代碼來定義可能的標記:

const StringWithoutQuote = createToken({ name: "StringWithoutQuote", pattern: /[\w!@#\$%\^&.-]+/ });
const StringDoubleQuote = createToken({ name: "StringDoubleQuote", pattern: /"[^"\\]*(?:\\.[^"\\]*)*"/ });
const StringSimpleQuote = createToken({ name: "StringSimpleQuote", pattern: /'[^'\\]*(?:\\.[^'\\]*)*'/ });
const And = createToken({ name: "And", pattern: /(AND|and)/ });
const Or = createToken({ name: "Or", pattern: /(OR|or)/ });
const WhiteSpace = createToken({
    name: "WhiteSpace",
    pattern: /[ \t\n\r]+/,
    group: Lexer.SKIPPED
});
const Colon = createToken({ name: "Colon", pattern: /:/ });
const Star = createToken({ name: "Star", pattern: /\*/ });

//.... Many other token

const allTokens = [
    WhiteSpace,
    Colon,
    Star,
    And,
    Or,

    //At this level I also tried to put StringWithoutQuote after the string tokens with quotes in the allTokens array

    StringWithoutQuote,
    StringDoubleQuote,
    StringSimpleQuote
];

我現在的問題:我將采用兩個示例字符串:

  • 聚合類型: *

  • orderInfo.orderDate: *

對於第一個字符串 ( aggregateType: *),無論使用何種語法(帶或不帶引號),解析器都能正常工作並返回預期結果。

但是對於第二個字符串( orderInfo.orderDate: ),帶引號的語法( 'orderInfo.orderDate':"orderInfo.orderDate": *)允許解析器正常工作並返回預期結果。

但是使用不帶引號的語法( orderInfo.orderDate: *),解析器會返回以下錯誤: Error: Failing to parse of string <orderInfo.orderDate:*> 我不確定,但我真的覺得這是加了一個點. 在導致錯誤的TERM部分中。 但是,在我的正則表達式( /[\w.@#\$%\^&.-]+/ )中,我確實放了點. 是我在令牌中要考慮的特殊字符之一。

有誰看到我做錯了什么導致這種行為?

如果您花時間幫助我,請提前致謝。

最后的解決方案是定義一個“更長的可能模式”,允許每次我檢查這個關鍵字的存在時說(在我的情況下)我還檢查它是否不是一個不太具體的標識符(在我的情況下是 StringWithoutQuote) .

更詳細的解釋在此鏈接的構造函數段落的末尾更詳細的解釋在文檔中

因此,詞法分析器的定義變為:

const StringWithoutQuote = createToken({ name: "StringWithoutQuote", pattern: /[\w!@#\$%\^&.-]+/ });
const StringDoubleQuote = createToken({ name: "StringDoubleQuote", pattern: /"[^"\\]*(?:\\.[^"\\]*)*"/ });
const StringSimpleQuote = createToken({ name: "StringSimpleQuote", pattern: /'[^'\\]*(?:\\.[^'\\]*)*'/ });
const And = createToken({ name: "And", pattern: /(AND|and)/, longer_alt: StringWithoutQuote });  //here you need to add loger_alt property
const Or = createToken({ name: "Or", pattern: /(OR|or)/, longer_alt: StringWithoutQuote });  //here you need to add loger_alt property
const WhiteSpace = createToken({
    name: "WhiteSpace",
    pattern: /[ \t\n\r]+/,
    group: Lexer.SKIPPED
});
const Colon = createToken({ name: "Colon", pattern: /:/ });
const Star = createToken({ name: "Star", pattern: /\*/ });
 
const allTokens = [
    WhiteSpace,
    Colon,
    Star,
    And,
    Or,
    StringWithoutQuote,
    StringDoubleQuote,
    StringSimpleQuote
];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM