首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本文件中查找特定结果

从文本文件中查找特定结果
EN

Stack Overflow用户
提问于 2014-02-04 05:23:53
回答 1查看 66关注 0票数 0

假设你正在阅读一个文本文件,使用Javascript和jQuery,假设服务器端的人不愿意给你,比如说xml或JSON,你想解析一次,以获得稍后将在自动补全中使用的相关文本,如下所示:

文本文件(假设有许多相似的列表,并且有不同的数据库):

代码语言:javascript
复制
QUERY:1
DATABASE:geoquery
NL:What are the capitals of the states that border the most populated states?
SQL:something
DR:
root(ROOT-0, What-1)
cop(What-1, are-2)
det(capitals-4, the-3)
nsubj(What-1, capitals-4)
det(states-7, the-6)
prep_of(capitals-4, states-7)
nsubj(border-9, states-7)
rcmod(states-7, border-9)
det(states-13, the-10)
advmod(populated-12, most-11)
amod(states-13, populated-12)
dobj(border-9, states-13)

QUERY:2
DATABASE:geoquery
NL:What are the capitals of states bordering New York?
SQL:SELECT state.Capital FROM state JOIN border_info ON state.State_Name        
DR:
root(ROOT-0, What-1)
cop(What-1, are-2)
det(capitals-4, the-3)
nsubj(What-1, capitals-4)
prep_of(capitals-4, states-6)
partmod(states-6, bordering-7)
nn(York-9, New-8)
dobj(bordering-7, York-9)

例如,我可以使用正则表达式来剥离所有NL:例如,但我需要首先削减文件,以便只读取与数据库关联的特定NL。因此,在获得用户从select中选择的特定数据库的所有匹配项后,读取该文件,然后从该列表中生成一个NL数组作为自动补全的源。

代码语言:javascript
复制
$(document).ready(function(){
        $.get('inputQueryExamples.txt',function(data){

            // need code here to read text file first and limit results 

            var queryString = data;
            var cleanString = "";
            cleanString = queryString.match(/^NL.*/gm);
            console.log(cleanString);      
            $('#what').html(cleanString);

            var nlString = cleanString.map(function(el) {return el.replace('NL:','');});




            $('#query-list').autocomplete({
                source:nlString
            });

        });//end get

     });

谢谢你的见解。

EN

回答 1

Stack Overflow用户

发布于 2014-02-04 06:21:04

使用正则表达式就像使用鸭带修补断肢一样。

不管怎样,

从外观上看,您希望获得来自特定数据库的所有NL('s)。

您需要执行多行正则表达式匹配,并对数据库名称进行正向查找,然后只需匹配NL之后的任何内容,并在下一个换行处停止。

示例:

(?<=DATABASE:geoquery).*?(?<=NL:)(.*?)(?=[\r\n])

在线演示:

Regex101 Example

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21538067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档