简体   繁体   English

在php中测试Java正则表达式

[英]test java regex in php

we are developing an android app and using some regex in that. 我们正在开发一个android应用,并在其中使用一些正则表达式。 I want to create some bulk unit tests at the server side to detect any issues before major releases.. for this purpose i need to mimic the java regex matching behavior on server side where i don't have access to java. 我想在服务器端创建一些大容量单元测试,以在主要版本发布之前检测到任何问题。为此,我需要在服务器端模仿Java regex匹配行为,因为我无法访问Java。 I am coding in php. 我在用php编码。 Is there any way to mimic java regex behavior without executing java or if there is any third party api (either free or paid) which I can call to get the result of java regex.. 有什么方法可以模仿Java正则表达式的行为而无需执行Java,或者是否可以调用任何第三方api(免费或付费)来获取Java正则表达式的结果。

PHP has a function called preg_match that will allow you to test a string against a regex pattern. PHP具有一个称为preg_match的函数,该函数可让您根据正则表达式模式测试字符串。 Re-use the Java regex patterns and do something like this: 重新使用Java regex模式并执行以下操作:

$yourPattern = '~^\d+$~'; // example pattern matching digits
if (preg_match($yourPattern, $someString)) {
    print 'Valid';
}

preg_match — Perform a regular expression match preg_match —执行正则表达式匹配

Usage 用法

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) int preg_match(字符串$ pattern,字符串$ subject [,数组&$ matches [,整数$ flags = 0 [,整数$ offset = 0]]])

Source 资源

http://php.net/preg_match http://php.net/preg_match

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

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