我有两个字符串:

http://porter.com/request/.*


http://porter.com/request/tokenId

我想检查一下第一部分:http://porter.com/request是否都相同,并检查tokenId是否不为null,因为在某些情况下,它可能仅仅是http://porter.com/request/
我用这样的东西:
override fun validate(pair: Pair<URI, URI>): Boolean {
        val uri = pair.first.path.split("/").dropLast(1).filter { it.isNotBlank() }.joinToString("")
        val uriIntent = pair.second.path.split("/").dropLast(1).filter { it.isNotBlank() }.joinToString("")

        val asd = pair.second.path.split("/").filter { it.isNotBlank() }.last().isNotBlank()

        return uri == uriIntent && asd
    }

但这不适用于最后一种情况:http://porter.com/request/有任何想法吗?

最佳答案

final String regex = "(http://porter.com/request/).+";

/**
 * Below code will return false
 * since, URL doesn't have last path
 */
final String yourUrl = "http://porter.com/request/.*";
final boolean valid = yourUrl.matches(regex)

/**
 * Same (will return false), as ex. above
 */

final String yourUrl = "http://porter.com/request/*";
final boolean valid = yourUrl.matches(regex)

/**
 * This will return true. Link is Ok.
 */

final String yourUrl = "http://porter.com/request/tokenId";
final boolean valid = yourUrl.matches(regex)

08-05 03:44