RegEx:为什么用逗号开始逗号分隔的字符串?

时间:2022-07-03 00:18:51

I was reading up on how to model tree structures in MongoDB and came across this article in which a comma-delimited path string is illustrated. In the example, the string starts with a comma and I was wondering if there was a reason or benefit for doing that apposed to just having a comma after each item in the list?

我正在阅读有关如何在MongoDB中建模树结构的内容,并且本文介绍了一个逗号分隔的路径字符串。在示例中,字符串以逗号开头,我想知道是否有一个原因或好处,只是在列表中的每个项目后面都有一个逗号?

The reason I marked this post as specific to RegEx is, in Java I would not want a comma at the beginning because I would parse the string into an array using...

我将此帖标记为特定于RegEx的原因是,在Java中我不想在开头使用逗号,因为我将使用...将字符串解析为数组

String[] array = string.split(",");

String [] array = string.split(“,”);

...and in PHP I would use...

...在PHP中我会用...

$array = explode(",", $string);

$ array = explode(“,”,$ string);

In either of those cases, starting the string with a comma would result in an empty string at $array[0].

在任何一种情况下,使用逗号启动字符串将导致$ array [0]处出现空字符串。

Thanks in advance!

提前致谢!

3 个解决方案

#1


2  

The example is not using a split type function rather its doing something that involves looking at characters in a string.

该示例不使用拆分类型函数,而是执行涉及查看字符串中的字符的操作。

The "guard" leading delimiter is so that if you wanted to find the single entry aaa in:

“守卫”前导分隔符是这样的,如果你想找到单个条目aaa:

",zzzaaa,bbb,aaa,zzz"

you can search for ,aaa, thus avoiding a false match of zzzaaa if you searched for aaa,.

如果你搜索aaa,你可以搜索,aaa,从而避免zzzaaa的错误匹配。

#2


2  

It is easier on the index to regex than conditionally doing /^[^,]SomeTopic,*

索引到正则表达式比有条件地做/ ^ [^,] SomeTopic,*更容易

#3


1  

In your referenced article, this has a benefit, because the regex /,Programming,/ is used. It simply searches for words that have a comma before and after the word. In case you leave the comma at the beginning of the string out, you'd additionally need to handle this issue, because Programming, would not be found by the RegEx.

在您的参考文章中,这有一个好处,因为使用了正则表达式/,编程,/。它只搜索单词前后有逗号的单词。如果你把逗号留在字符串的开头,你还需要处理这个问题,因为RegEx找不到编程。

#1


2  

The example is not using a split type function rather its doing something that involves looking at characters in a string.

该示例不使用拆分类型函数,而是执行涉及查看字符串中的字符的操作。

The "guard" leading delimiter is so that if you wanted to find the single entry aaa in:

“守卫”前导分隔符是这样的,如果你想找到单个条目aaa:

",zzzaaa,bbb,aaa,zzz"

you can search for ,aaa, thus avoiding a false match of zzzaaa if you searched for aaa,.

如果你搜索aaa,你可以搜索,aaa,从而避免zzzaaa的错误匹配。

#2


2  

It is easier on the index to regex than conditionally doing /^[^,]SomeTopic,*

索引到正则表达式比有条件地做/ ^ [^,] SomeTopic,*更容易

#3


1  

In your referenced article, this has a benefit, because the regex /,Programming,/ is used. It simply searches for words that have a comma before and after the word. In case you leave the comma at the beginning of the string out, you'd additionally need to handle this issue, because Programming, would not be found by the RegEx.

在您的参考文章中,这有一个好处,因为使用了正则表达式/,编程,/。它只搜索单词前后有逗号的单词。如果你把逗号留在字符串的开头,你还需要处理这个问题,因为RegEx找不到编程。