-3

I am trying to make regex that will check if certain characters are in a string. For instance if I have the string "example" I want to be able to decide if the characters e, l and a are in the string. Can I do this with a regex?

Furthermore it's important that if the regex contains the characters e, e, l and a that the string has two instances of e before it returns true.

Thanks in advance

Pshemo
  • 113,402
  • 22
  • 170
  • 242
sqdejan
  • 71
  • 5
  • 6
    Can you show us your attempt ? – Kakarot Apr 25 '14 at 18:50
  • 2
    Does it have to be a regex? Problems where you count or look for occurrences of specific characters are often better handled with a loop. – ajb Apr 25 '14 at 18:53
  • Fo sho. If I have: String myStr = "example" I do like this myStr.matches("[exampl]*" and get true. However if I remove the l in the regex it will not return true, and that is what I need. – sqdejan Apr 25 '14 at 18:58
  • I just want to avoid making an insane loop because I will have to remove characters in order to be able to determine if there are two occurences of the letter if needed. – sqdejan Apr 25 '14 at 18:59
  • Better go directly to the [java Pattern page](http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html). Still, your example has exponential growth for the patternstring itself. – Deduplicator Apr 25 '14 at 18:59
  • If you insist on using a pattern, you need start- and end-anchors `^$`, alternatives `|`, groups `()`, and match for run of any length of any characters `.*`. Also, the letters `ael` only match themselves. Have fun constructing. – Deduplicator Apr 25 '14 at 19:03
  • I need it to be dynamic so it's not only for ael, however I just wanted to know if there was an easy way instead of making a loop based checker. Thanks for your help. I have done enough lexical analysis to know that I would first be finished in 2156. :D – sqdejan Apr 25 '14 at 19:11
  • 1
    If you are interested in regex approach then take a look at this question: http://stackoverflow.com/questions/3802192/regexp-java-for-password-validation – Pshemo Apr 25 '14 at 19:12
  • @Deduplicator's comment, [FAQ](http://stackoverflow.com/a/22944075/2736496)-ified: If you insist on using a pattern, you need [start- and end-anchors `^$`](http://stackoverflow.com/a/22759143/578411), [alternatives `|`](http://stackoverflow.com/a/22187948), [groups `()`](http://stackoverflow.com/q/21880127), and match for run of [any length of any characters `.*`](http://stackoverflow.com/a/10764399). Also, the letters `ael` only match themselves. Have fun constructing. – aliteralmind Apr 25 '14 at 19:13
  • Please consider bookmarking the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496) for future reference. – aliteralmind Apr 25 '14 at 19:14

3 Answers3

0

Big Edit:

I take back what i said about using regex and matcher, seems to complicated. If you are looking to just check if all three are present in a string you should cast the string to a chararray then check each character individually against any characters you are checking for

String myString = "testString";
char[] myCharArray = myString.toCharArray();

Next by looping through the array using a for loop you are able to check each individual letter

for (int i = 0; i < myString.length(); i++) {

if myCharArray[i] = 'a' {
aboolean = true; 
}

if myCharArray[i] = 'e' {
eboolean = true; 
}

}
Mike Docherty
  • 169
  • 2
  • 13
  • This is just general basic information about regexes, and that isn't good enough to solve more complicated problems. And this one is a complicated problem even though it doesn't look like it. – ajb Apr 25 '14 at 18:58
  • @ajb: It's not complicated in any way, just tedious. – Deduplicator Apr 25 '14 at 19:00
  • @Deduplicator I think it would be complicated (or at least non-obvious) for those who only know the basics about regexes. – ajb Apr 25 '14 at 19:01
  • @Deduplicator Level of difficulty is based on knowledge about regex. For someone new to this subject, who doesn't know about look-ahead mechanisms solving this problem with regex could seem impossible. – Pshemo Apr 25 '14 at 19:05
  • I have updated this and simplified hugely due to feedback on my answer – Mike Docherty Apr 25 '14 at 19:12
  • `myString.length()` needs parens. `myCharArray.length` doesn't. – ajb Apr 25 '14 at 19:14
  • Thats for catching that, just wrote it out in the comment box as opposed to in eclipse, totally missed it – Mike Docherty Apr 25 '14 at 19:17
0

yourString.matches("(.*e.*)|(.*l.*)|(.*a.*)";

This will return true if the String contains "e", "l" or "a".

".*" in a regex matches anything any number of times. e obviously only matches e. | is logical or. wrap it all in ""and group expressions on either side of |for success.

Furthermore it's important that if the regex contains the characters e, e, l and a that the string has two instances of e before it returns true.

yourString.matches("(?=.*l.*)(?=.*a.*).*e.*e.*");

this matches anything with two e's, an l and an a.

  • `"(.*e.*)|(.*l.*)|(.*a.*)"` regex will also accept string which contain only one of `e`, `l` or `a` characters. Problem is little more complicated because OP requires them all to exist in string (possibly regardless of order). – Pshemo Apr 25 '14 at 19:10
  • Your second attempt will only match strings with `e`, `e`, `l`, `a` **in that order**, which will fail on `"example"`. – ajb Apr 25 '14 at 19:12
  • Ah didnt catch that he didnt want them ordered. will fix. also @Pshemo that first regex is meant as an example – Hans Petter Taugbøl Kragset Apr 25 '14 at 19:21
0

If you want to match any string that contains two or more e's, anywhere in the string, this regex would do it:

.*e.*e.*

But extending this to look for other characters, that could be anywhere in the string relative to those two e's, isn't trivial. You can do it with a lookahead:

(?=.*l)(?=.*a).*e.*e.*

which matches if, at some point in the source string, the matcher can find all of the patterns .*l, .*a, and .*e.*e.* starting from that point. This will tell if there's one l, one a, and at least two e's, in any order. (If you want to look for, say, two l's, then something like this will work:)

(?=.*l.*l)(?=.*a).*e.*e.*

However, this requires that the matcher perform three scans starting at each point in the regex. That's why I believe problems like this are better solved with a loop:

eCount = 0;
aCount = 0;
lCount = 0;
for (int i = 0; i < s.length(); i++) {
   switch(s.charAt(i)) {
      case 'e':
         eCount++;  
         break;
      case 'a':
         aCount++;
         break;
      case 'l':
         lCount++;
         break;
   }
}
if (eCount >= 2 && aCount >= 1 && lCount >= 1) { ... }

More verbose, but overall I think more efficient.

ajb
  • 29,914
  • 3
  • 49
  • 73