tags:

views:

55

answers:

2

I know exact prefix of the String.

E.g. it is 'XXX000'

After the prefix, digits and chars in quantity of 60 go.

How to construct regexp this case?

In my initial understanding it should looks like:

(XXX000)(\w{*})

like: prefix(some digits or some chars)

Thank you.

+5  A: 

Use this /XXX000(\w{0,60})/

/    <- Start of the regex
    XXX000    <- Your constant prefix (don't really need to capture it then)
    (         <- Capture the following matching elements
        \w        <- Every characters in [a-zA-Z0-9_]
        {0,60}    <- Between 0 and 60 repetitions of the last element

    )         <- End of the group
/    <- End of the regex

If you don't want the [a-zA-Z0-9_] chars, replace by your own character class.

Note : You may not need delimiters, remember to remove them if it's the case.


Resources :

Colin Hebert
Java not Javascript/Perl/etc...forward slashes wrong.
Mark Peters
thanks a lot. works!
sergionni
@Mark Peters, didn't notice the java tag, but I always give regex in this form. I'll add a comment.
Colin Hebert
actually,i build regexp in .xml bean part as parameter of bean(Spring), so value="(XXX000)(\w{0,74})" works for me
sergionni
@sergio If you don't care (or don't know) about quantity, you can use the `*` (zero or more) or the `+` (one or more) repetition operators instead of `{n, m}`
NullUserException
A: 

if you need match exactly 60 chars, it's XXX000\w{60}

it's unnecessary to group with the parens unless you need to capture the part of the match. http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

farble1670