1

I have two conditions in my regular expression. Say,

  1. (.*text.*) ---> which will check whether the input string has the substring "text" in it.
  2. (^((?!query).)*$) ----> which will check whether the input string does not contains the word "query" int it.

Both are working fine for me.

But I want a regular expression to check both the conditions using something like AND operator.

I want a regex which should return true only if the input string contains the substring "text" and does not contains "query" in it.

I got the info that regex does not support AND operator. So, I tried to do something like the following:

NOT(NOT(expression 1)|(NOT(expression 2)))

eg:- (!(.*query.*)|(^((?!text).)*$))

But even this does not work for me..

Anyone please help me regarding this.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • Java or Javascript or both? It's not the same. If it's just static texts you want to check, why not use "contains": (!stringToCheck.contains(text) && !stringToCheck.contains(query)) – Adrian Mar 05 '13 at 07:15
  • Apparently the target flavor is Java. I removed the other tags. – Alan Moore Mar 12 '13 at 11:17

1 Answers1

2

Use a non-consuming regular expression.

The typical notation is:

(?=expr)

This means match expr but after that continue matching at the original match-point.

You can do as many of these as you want, and this will be an "and".

Syntax Eg:

(?=match this expression)(?=match this too)(?=oh, and this)

Update:-

Need to add .* incase matches() is used so that regex matches the entire string.

Your regex will be str.matches("(?=(.*text.*))(?=(^((?!query).)*$)).*")

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • This is not an "and", this is just a concatenation. How could you evaluate "someString" to false with this, when text=meSt and query=meSt? – Adrian Mar 05 '13 at 07:18
  • @Adrian - Here is a reference for more a detailed [explanation](http://stackoverflow.com/questions/6185438/and-operator-in-regular-expressions) – Rahul Mar 05 '13 at 07:21
  • Thanks R.J, I tried (?=(.*text.*))(?=(^((?!query).)*$)) but it is not working for me. Sorry I am a novice in regex. Please correct me if I am wrong. – Mohan Suresh Ramasamy Mar 05 '13 at 07:23
  • happy to help :) accept the answer if it served your purpose! – Rahul Mar 05 '13 at 07:50
  • @R.J Thanks for your help.. It is working if we use str.matches but the code is like "matcher = regexp.matcher(str);" and when using "matcher.find()" in if condition it is not working :( I could not modify the code. – Mohan Suresh Ramasamy Mar 05 '13 at 09:15
  • It should be `matcher.matches()` and not `matcher.find()`. – Rahul Mar 05 '13 at 09:18
  • @R.J The actual problem is i have to find a word which matches the above regex and i have to redirect to some other url in my application server. So, i need the to get the word which matches the regex from the incoming URL (which will be typically enclosed with /). That is why we are using matcher.find(). And the problem is the code cannot be modified now whereas i can only modify the input string (the URL in my case) and the regex pattern. :( – Mohan Suresh Ramasamy Mar 05 '13 at 09:33
  • `String str = "textquer"; Pattern pattern = Pattern.compile("(?=(.*text.*))(?=(^((?!query).)*$)).*"); Matcher matcher = pattern.matcher(str); if (matcher.find()) { // The below String has the text which matches this pattern. String result = matcher.group(1); System.out.println(result); }` – Rahul Mar 05 '13 at 09:37