简体   繁体   English

文本输入中用于自定义语法的正则表达式

[英]Regular expression for custom syntax in text input

I'm supposed to enforce a certain search-syntax in a text input, and after watching several RegEx videos and tutorials, I'm still having difficulties creating a regex for my purpose. 我应该在文本输入中强制使用某种搜索语法,并且在观看了多个RegEx视频和教程之后,为实现我的目的创建正则表达式仍然很困难。 The expression structure should be something like that: $earch://site.com?y=7, app=app1, wu>7, cnt<>8, url=http://kuku.com?adasd=343 , p=8 表达式结构应类似于: $earch://site.com?y=7, app=app1, wu>7, cnt<>8, url=http://kuku.com?adasd=343 , p=8

may start with a free text search that may contain any character other than the delimiter, which is , . 可以用自由文本搜索可能含有比分隔符,这是其他任何字符开始, (free text must be first, and the string may be ONLY free text search). (必须先输入自由文本,并且字符串只能是自由文本搜索)。

after free text comma-separated parts of field names which consist only [az][AZ] , followed by operator: (=|<|>|<>) and followed by field search value that may be anything but , . 字段名称的自由文本逗号分隔部分之后,该部分仅由[az][AZ] ,后跟运算符: (=|<|>|<>) ,然后是字段搜索值,该值可能不是,

between the commas that separate the parts there may be spaces (\\s*) . 分隔各部分的逗号之间可能有空格(\\s*)

The free text part or at least one field=value must appear in order for the string to be valid. 必须显示自由文本部分或至少一个field = value才能使字符串有效。

Did anyone understand the question? 有人知道这个问题吗? :) :)

^[^,]*(?:,\\s*[a-zA-Z]+(?:[=><]|<>)[^,]+)*$ ? ^[^,]*(?:,\\s*[a-zA-Z]+(?:[=><]|<>)[^,]+)*$ – Rawing –生皮

Thanks, that seems to work. 谢谢,这似乎有效。 Why did you use non-capturing groups? 为什么要使用非捕获组?

He did it most probably because he didn't assume that the groups are to be captured (you didn't specify that). 他之所以这样做,很可能是因为他没有假设要捕获这些组(您未指定)。

Plus - if I start out the string with a comma, it is valid, whereas I want it to not be valid (if there's no free text at the beginning). 另外-如果我以逗号开头的字符串是有效的,而我希望它是无效的(如果开头没有自由文本)。

That can be accomplished by changing the first * to a + , ie ^[^,]+… 可以通过将第一个*更改为+来实现,即^[^,]+…

I'm using javascript. 我正在使用javascript。 I want to be able to separate each key=value pair (including the possible free text as a group), and within that group I would like to be able to capture key, operator, and value as separate entities (or groups) 我希望能够将每个键=值对(包括可能的自由文本作为一个组)分开,并且在该组中,我希望能够将键,运算符和值捕获为单独的实体(或组)

That's not doable with only one RegExp invocation, see eg How to capture an arbitrary number of groups in JavaScript Regexp? 仅使用一个RegExp调用是无法实现的,请参见例如, 如何在JavaScript Regexp中捕获任意数量的组? Here's an example solution: 这是一个示例解决方案:

 s = '$earch://site.com?y=7, app=app1, wu>7, cnt<>8, url=http://kuku.com?adasd=343 , p=8' part = /,\\s*([a-zA-Z]+)(<>|[=><])([^,]+)/ re = RegExp('^([^,]+)('+part.source+')*$') freetext = re.exec(s)[1] // validate s and take free text as 1st capture group of re if (freetext) { document.write('free text:', freetext, '<p>') parts = RegExp(part.source, 'g') m = s.slice(freetext.length).match(parts) // now get all key=value pairs into m[] if (m) { field = [] for (i = 0; i < m.length; ++i) { f = m[i].match(part) // and now capture key, operator and value from m[i] field[i] = { key:f[1], operator:f[2], value:f[3] } for (property in field[i]) // display them document.write(property, ':', field[i][property], '; ') document.write('<p>') } document.write(field.length, ' key/value pairs total<p>') } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM