0

I am not a dab at regulars but I have a task at hand and I think expressions might be handy. I want to check if a string like 'stringList[0]', 'stringList[2]' fits the pattern, that is I am trying to figure out whether a string is a textual representation of a collection's element. What would that pattern look like? I will use java as a primary tool. Thank you so much in advance.

2 Answers 2

2

To match for 'stringList[0]' you can use

"stringList\\[\\d+\\]"

which matches stringList, followed by [ and the at least one digit followed by ]

Edit:

To match what was clarified

"[\\p{L}_$][\\p{L}\\p{N}_$]*\\[\\d+\\]"

which I think matches any valid identifier. Starting with with any letter, _ or $ and then followed by one or more letter, number, _ or $ followed by [, then 1 or more digits and then another ].

Sign up to request clarification or add additional context in comments.

1 Comment

It was an example. Sorry, I might have not made myself clear. I want to match any string like 'stringEx[1]', 'lion[76]', 'nocturnalBird[199]'. A pattern that matches all of them.
2
"stringList\\[(\\d+)\\]"

would match "stringList[a number]"

"([a-zA-Z]+)\\[(\\d+)\\]"

would match any sequence of upper/lowercase characters followed by "[a number]" legal java class/field names may contain numbers (not as a first character) and underscores as well (and maybe more things i cant think of?), so you'd need something more complex for the 1st block, but you could probbaly google for a "full" expression for legal java class/field names

2 Comments

It was an example. Sorry, I might have not made myself clear. I want to match any string like 'stringEx[1]', 'lion[76]', 'nocturnalBird[199]'. A pattern that matches all of them.
i've added a 2nd, more general regex (still not 100% complete)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.