-2

I have 2 separate strings "TM_TASK" and "TM_CHECKLIST". How can i validate both the words in whole using regex in java ? How to use AND operator in regex for this case in java ? i need both the strings to be validated and not either of them. I am new to regex.

@Pattern(value = "(?=.*TM_TASK)(?=.*TM_CHECKLIST).*", patternType = PatternType.REGEX)  
@ApiOperation(value = "Fetch all instances of a specific checklist", produces = "application/json", response = ChecklistInstance.class, tags = "tasks")
@ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "Authorization token", required = true, dataType = "string", paramType = "header"),
        @ApiImplicitParam(name = "body", value = "json document", required = true, dataType = "json", paramType = "body") })
@BodyParser.Of(BodyParser.Json.class)
public Result getChecklistInstances() {
Sourav Bebarta
  • 51
  • 2
  • 10

2 Answers2

1

you can use the regex

(?=.*TM_TASK)(?=.*TM_CHECKLIST).*

to match if the string contains both TM_TASK and TM_CHECKLIST, see the regex101 demo. Once this is done you can match those words using the regex

TM_(?:TASK|CHECKLIST)

see the regex101 demo

marvel308
  • 10,288
  • 1
  • 21
  • 32
  • No they are not working. And i didn't mean in the question that both words are part of a line/sentence. They are entire separate words.And they should be matched in whole. And both words should be matched at the same time. – Sourav Bebarta Sep 01 '17 at 11:29
  • @SouravBebarta, I don’t understand the difference between what you are requiring and what this answer is giving you. Could you spell it out more clearly? Preferably in the question. – Ole V.V. Sep 01 '17 at 11:34
  • you can use my regex to verify if both exist in the string and Christian Müller answer to match them – marvel308 Sep 01 '17 at 11:35
  • @marvel308 Christian Muller answer has regex validation on OR condition. But i want both TM_TASK and TM_CHECKLIST to be present but not as part of 1 string. They are separate strings completely. – Sourav Bebarta Sep 01 '17 at 11:49
  • @SouravBebarta: This does **exactly** what you want. Did you try it? – Toto Sep 01 '17 at 11:53
  • @Toto Yes of course i tried. I will reiterate again. The answer works if both the words TM_TASK and TM_CHECKLIST are part of 1 string.But in my case as mentioned in my question, both the words are 2 separate strings. – Sourav Bebarta Sep 01 '17 at 12:13
  • @SouravBebarta: That's really not clear at all, do you want to match both words or strings (it doesn't matter) or not?. What do you mean by "both the words are 2 separate strings"? Please, add some test cases in your question with expected result. – Toto Sep 01 '17 at 12:23
0

RegEx:

^TM_(TASK|CHECKLIST)$

in Java:

String.matches("^TM_(TASK|CHECKLIST)$");

Find manual at RegExJava

Chris
  • 158
  • 8