java中用空格标记字符串
我想标记这样的字符串
String line = "a=b c='123 456' d=777 e='uij yyy'";
我不能像这样分割
String [] words = line.split(" ");
任何想法如何分割以便我得到像这样的标记
a=b
c='123 456'
d=777
e='uij yyy';
I want to tokenize a string like this
String line = "a=b c='123 456' d=777 e='uij yyy'";
I cannot split based like this
String [] words = line.split(" ");
Any idea how can I split so that I get tokens like
a=b
c='123 456'
d=777
e='uij yyy';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
最简单的方法是手动实现一个简单的有限状态机。换句话说,一次处理一个字符的字符串:
The simplest way to do this is by hand implementing a simple finite state machine. In other words, process the string a character at a time:
根据原始字符串的格式,您应该能够使用正则表达式作为 java“split”方法的参数: 单击此处查看示例。
不过,该示例并未使用此任务所需的正则表达式。
您还可以使用 这个SO线程作为指南(尽管它是用PHP编写的),它所做的事情非常接近您所需要的。稍微进行一些操作可能会达到目的(尽管引号是否成为输出的一部分可能会导致一些问题)。请记住,正则表达式在大多数语言中都非常相似。
编辑:对此类任务进行过多深入研究可能会超出正则表达式的功能,因此您可能需要创建一个简单的解析器。
Depending on the formatting of your original string, you should be able to use a regular expression as a parameter to the java "split" method: Click here for an example.
The example doesn't use the regular expression that you would need for this task though.
You can also use this SO thread as a guideline (although it's in PHP) which does something very close to what you need. Manipulating that slightly might do the trick (although having quotes be part of the output or not may cause some issues). Keep in mind that regex is very similar in most languages.
Edit: going too much further into this type of task may be ahead of the capabilities of regex, so you may need to create a simple parser.
正确给出:
确保调整 [a-z+] 部分,以防您的键结构发生变化。
编辑:如果该对的值部分中有“=”字符,则此解决方案可能会严重失败。
correctly gives:
Make sure you adapt the [a-z+] part in case your keys structure changes.
Edit: this solution can fail miserably if there is a "=" character in the value part of the pair.
StreamTokenizer 可以提供帮助,尽管它是最简单的设置在“=”处中断,因为它总是在带引号的字符串的开头处中断:
输出
如果省略将数字字符转换为字母的两行,则会得到
d=777.0
,这可能对您有用。StreamTokenizer can help, although it is easiest to set up to break on '=', as it will always break at the start of a quoted string:
outputs
If you leave out the two lines that convert numeric characters to alpha, then you get
d=777.0
, which might be useful to you.假设:
这对我来说效果很好。
输入:
输出:
代码:
Assumptions:
This works fine for me.
Input:
Output:
Code:
或者,使用用于标记化的正则表达式,以及一个仅将键/值添加到映射的小型状态机:
打印输出
它会进行一些基本的错误检查,并从值中去掉引号。
Or, with a regex for tokenizing, and a little state machine that just adds the key/val to a map:
prints out
It does some basic error checking, and takes the quotes off the values.
这个解决方案既通用又紧凑(它实际上是 cletus 答案的正则表达式版本):
换句话说,找到所有由带引号的字符串或非空格字符组合而成的字符;不支持嵌套引号(没有转义字符)。
This solution is both general and compact (it is effectively the regex version of cletus' answer):
In other words, find all runs of characters that are combinations of quoted strings or non-space characters; nested quotes are not supported (there is no escape character).
输出:
{d=777, a=b, e='uij yyy', c='123 456'}
在这种情况下,连续空格将被截断为值中的单个空格。
这里属性哈希图包含值
output:
{d=777, a=b, e='uij yyy', c='123 456'}
In this case continuous space will be truncated to single space in the value.
here attributed hashmap contains the values
您是否尝试过按“=”拆分并从每对结果数组中创建一个标记?
Have you tried splitting by '=' and creating a token out of each pair of the resulting array?