时间:2011-08-23 12:21:09

标签: java regex string debugging matcher

我正在通过eclipse调试器运行一些代码,当a[1].matches("[a-zA-Z]")true是字符串数组时)a[1] = "ABCD"不等于a

我已阅读matches上的javadoc,而[a-zA-Z]应该是有效的正则表达式。

任何人都知道我哪里出错了?

6 个答案:

答案 0 :(得分:6)

尝试使用此表达式:[a-zA-Z]*(将匹配零个或多个字符)。

如果您需要至少一个字符,请使用:[a-zA-Z]+

您使用的表达式只匹配单个字母字符,因为它不是quantified

答案 1 :(得分:2)

试试a[1].matches("[a-zA-Z]+")。它说“一个或多个字符”必须匹配,而不是只匹配一个字符。

请注意,'*'代替'+'匹配“零个或多个字符”,因此它将匹配空字符串(可能不是您想要的)。

答案 2 :(得分:1)

我认为它应该是a[1].matches("[a-zA-Z]*")

答案 3 :(得分:0)

[a-zA-Z]只接受一封信。您可能需要[a-zA-Z]*

答案 4 :(得分:0)

您不匹配字符串的原因是您的RegEx表达式尝试仅匹配单个字符。试试这个:

a[1].matches("[a-zA-Z]*")

答案 5 :(得分:0)

type Expr =
    | Lit of int
    | Add of Expr * Expr

let rec intr = function
    | Lit _ as x -> x
    | Add(Lit a,Lit b) -> Lit <| a + b
    | Add(a,b) -> intr <| Add(intr a, intr b)

let rec intr_cps x ret =
    match x with
    | Lit _ as x -> ret x
    | Add(Lit a,Lit b) -> Lit (a + b) |> ret
    | Add(a,b) -> 
        intr_cps a <| fun a ->
            intr_cps b <| fun b ->
                intr_cps (Add(a, b)) ret

let rec add n =
    if n > 1 then Add(Lit 1, add (n-1))
    else Lit 1

open System.Threading

let mem = 1024*1024*512 // ~536mb
// It stack overflows without being spun on a separate thread.
// By default, the program only has a few mb of stack memory at its disposal.
let run f = Thread(ThreadStart f,mem).Start() 

run <| fun _ ->
    let f n =
        let x = add n
        let stopwatch = System.Diagnostics.Stopwatch.StartNew()
        printfn "%A" (intr x)
        printfn "n_%i_std = %A" n stopwatch.Elapsed

        stopwatch.Restart()
        printfn "%A" (intr_cps x id)
        printfn "n_%i_cps = %A" n stopwatch.Elapsed
    f <| 1000*1000/2
    f <| 1000*1000
    f <| 1000*1000*2

//Lit 500000
//n_500000_std = 00:00:00.7764730
//Lit 500000
//n_500000_cps = 00:00:00.0800371
//Lit 1000000
//n_1000000_std = 00:00:02.9531043
//Lit 1000000
//n_1000000_cps = 00:00:00.1941828
//Lit 2000000
//n_2000000_std = 00:00:13.7823780
//Lit 2000000
//n_2000000_cps = 00:00:00.2767752

可能有帮助