-5

I want to identify all strings that contain the number 5 after finger_ or ground_. This should ignore the number at the end (regardless of if it ends with phase_ or env_).

Here are some examples:

finger_5_ground_4_phase_92 = YES
finger_1_ground_2_phase_9 = NO
finger_4_ground_2_phase_5 = NO
finger_4_ground_2_env_5 = NO
finger_4_ground_5_env_5 = YES
Barmar
  • 741,623
  • 53
  • 500
  • 612
Rohan
  • 515
  • 10
  • 30

2 Answers2

0

Use:

    Pattern pattern = Pattern.compile(".*(?:finger|ground)_5.*");
    System.out.println(String.format("%s = %s", "finger_5_ground_4_phase_92", pattern.matcher("finger_5_ground_4_phase_92").matches() ? "YES" : "NO"));
    System.out.println(String.format("%s = %s", "finger_1_ground_2_phase_9", pattern.matcher("finger_1_ground_2_phase_9").matches() ? "YES" : "NO"));
    System.out.println(String.format("%s = %s", "finger_4_ground_2_phase_5", pattern.matcher("finger_4_ground_2_phase_5").matches() ? "YES" : "NO"));
    System.out.println(String.format("%s = %s", "finger_4_ground_2_env_5", pattern.matcher("finger_4_ground_2_env_5").matches() ? "YES" : "NO"));
    System.out.println(String.format("%s = %s", "finger_4_ground_5_env_5", pattern.matcher("finger_4_ground_5_env_5").matches() ? "YES" : "NO"));
Ulises
  • 9,115
  • 2
  • 30
  • 27
-2

Use this:

.*(finger|ground)_5_.*
vinycd
  • 97
  • 4