[英]Regex to check for new line
I want to check if an if statement is on one line or the next line without a brace like so: 我想检查if语句是在一行还是下一行没有括号如下:
if (blah === blah)
do something
or: 要么:
if (foo === foo) do something
The regex i have currently is 我目前的正则表达式是
/\)(?!.*\{)/
But doesnt work. 但不起作用。 Anyone have any ideas?
有人有主意吗?
To elaborate the only If statement that would not be pulled by this regex is the following: 要详细说明此正则表达式不会提取的唯一If语句如下:
if (foo === bar)
{
简单\\r
\\n
(回车和换行)
/[\r\n]/
New lines may be \\n
(UNIX), \\r\\n
(Windows), \\r
(for good measure). 新行可能是
\\n
(UNIX), \\r\\n
(Windows), \\r
\\r\\n
(为了更好的衡量标准)。 The expression to broadly match new lines is: 广泛匹配新行的表达式是:
/\r\n|\r|\n/
To check if an expression is on one line, you can do this: 要检查表达式是否在一行上,您可以执行以下操作:
if( preg_match( '/\r\n|\r|\n/', $string ) ) {
// on more than one line
...
} else {
// on one line
...
}
To further develop this to apply to your specific example of an if ... do
statement, you could do this: 要进一步开发它以应用于
if ... do
语句的特定示例,您可以这样做:
if( preg_match( '/if *\([^\)]+\) *(\r\n|\r|\n) *do /', $string ) ) {
// on more than one line
...
} elseif( preg_match( '/if *\([^\)]+\) *do /', $string ) ) {
// on one line
...
}
You need to make .
你需要做
.
match newlines, too: 也匹配换行符:
/\)(?!.*\{)/s
^
PCRE_DOTALL Modifier
This is done with the s
( PCRE_DOTALL
) modifier (as written in comments): 这是通过
s
( PCRE_DOTALL
)修饰符完成的 (如注释中所述):
s (PCRE_DOTALL)
s(PCRE_DOTALL)
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines.
如果设置了此修饰符,则模式中的点元字符将匹配所有字符,包括换行符。 Without it, newlines are excluded.
没有它,排除了换行符。 This modifier is equivalent to Perl's /s modifier.
此修饰符等效于Perl的/ s修饰符。 A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
诸如[^ a]之类的负类始终匹配换行符,与此修饰符的设置无关。
As you want to check this specifically for an if
, use this: 如果您想专门针对
if
进行检查,请使用以下命令:
(if[ ()\w=]+)\r\n
returns true if the if
has a newline and false if not. 如果
if
有换行符if
返回true,否则返回false 。
You are looking for if statements that don't use curly braces, but your pattern requires curly braces. 您正在寻找不使用花括号的if语句,但您的模式需要花括号。
Here is my suggestion: ( Demo ) 这是我的建议:( 演示 )
$strings = [
'if (blah === blah)
do something',
'if (foo === foo) do something',
'if (bah === bah) {
do something
}',
'if (bar === bar) {do something}'
];
foreach ($strings as $string) {
var_export(preg_match('~if\s*\(.*?\)\s*(\{)?~', $string, $m) ? $m : '');
echo "\nHas curly brace: " , isset($m[1]) ? 'Yes' : 'No';
echo "\n---\n";
}
Output: 输出:
array (
0 => 'if (blah === blah)
',
)
Has curly brace: No
---
array (
0 => 'if (foo === foo) ',
)
Has curly brace: No
---
array (
0 => 'if (bah === bah) {',
1 => '{',
)
Has curly brace: Yes
---
array (
0 => 'if (bar === bar) {',
1 => '{',
)
Has curly brace: Yes
---
Basically, use \\s*
to signify where no space/newlines, a space/newline, multiple spaces/newlines may occur in the markup. 基本上,使用
\\s*
表示标记中可能没有空格/换行符,空格/换行符,多个空格/换行符。
My pattern will not catch if
statements with multi-line expressions. 我的模式不会捕获具有多行表达式的
if
语句。 To accommodate those fringe cases, add the s
pattern modifier to allow the .
为了适应这些边缘情况,添加
s
模式修改器以允许.
to match newlines. 匹配换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.