我试图在php中替换以下字符串:
2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres 46521,Pyroxeres
使用:
2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres,46521,Pyroxeres
注意:这里有5个逗号分隔值
Time, Character, Item Type, Quantity, Item Group
请注意,仍然会留下一些空格,例如日期和时间以及名字和名字以及名称中包含多个单词的项目。
此外,它不能取代\ r和\ n,因为有很多行,如上面的
答案 0 :(得分:1)
以下是搜索的正则表达式:
(\d{4}\.\d{2}\.\d{2}\s\d{2}:\d{2},[^,]*,\D*)\s(\d{5},.*)
替换正则表达式
$1,$2
<强>解释强>
搜索正则表达式
( Begin a numbered capture group (Nameless and accessed via index)
\d{4} Means: capture digits with exact 4 repititions
\. Capture a dot (You have to escape it with a backslash, if not it means capture all
characters except linefeed)
\d{2} Capture 2 digits
\. Capture a dot
\d{2} Capture 2 digits
\s Capture 1 whitespace
\d{2} Capture 2 digits
: Capture a colon
\d{2} Capture 2 digits
, Capture a comma
[^,]* Capture everything except a comma with 0 to infinity repititions
, Capture a comma
\D* Capture everything except a digit with 0 to infinity repititions
) Close first capture group
\s Capture a whitespace
( Begin a numbered capture group
\d{5} Capture 5 digits
, Capture a comma
.* Capture everything except a linebreak
) Close second capture group
替换的正则表达式
$1 Insert capture group number 1
, Insert a comma
$2 Insert capture group number 2
我希望这能解释正则表达式的一些(有用但有时令人困惑)“魔力”。设计和测试正则表达式的好工具叫做Expresso,可以找到here。如果你想要一个带有表达式测试器的在线帮助和正则表达式库,你会发现它here
答案 1 :(得分:0)
您可以使用此
$result = preg_replace('/(?<=[a-z_])\s+(?=\d)|(?<=\d)\s+(?=[a-z_])/i', ',', $subject);
<强>解释强>
# Match either the regular expression below (attempting the next alternative only if this one fails)
(?<= # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
[a-z_] # Match a single character present in the list below
# A character in the range between “a” and “z”
# The character “_”
)
\s # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
\d # Match a single digit 0..9
)
| # Or match regular expression number 2 below (the entire match attempt fails if this one fails to match)
(?<= # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
\d # Match a single digit 0..9
)
\s # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
[a-z_] # Match a single character present in the list below
# A character in the range between “a” and “z”
# The character “_”
)
答案 2 :(得分:0)
在Perl中你可以使用:
$line =~ s/([A-Za-z])\s+(\d)/$1,$2/g;
答案 3 :(得分:0)
你没有说你正在使用什么语言,但这样的事情应该有效:
搜索:([A-Za-z]) (\d)
替换:\1,\2
答案 4 :(得分:0)
怎么样:
$str = "2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres 46521,Pyroxeres";
$str = preg_replace('/(\D+) (\d+)/', "$1,$2", $str);
echo $str;
<强>输出:强>
2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres,46521,Pyroxeres