8

Can anyone please help me do the following in a java regular expression?

I need to read 3 characters from the 5th position from a given String ignoring whatever is found before and after.

Example : testXXXtest

Expected result : XXX

1
  • why regex? did you try substring Commented Dec 6, 2011 at 19:25

5 Answers 5

20

You don't need regex at all.

Just use substring: yourString.substring(4,7)

Since you do need to use regex, you can do it like this:

Pattern pattern = Pattern.compile(".{4}(.{3}).*");
Matcher matcher = pattern.matcher("testXXXtest");
matcher.matches();
String whatYouNeed = matcher.group(1);

What does it mean, step by step:

.{4} - any four characters

( - start capturing group, i.e. what you need

.{3} - any three characters

) - end capturing group, you got it now

.* followed by 0 or more arbitrary characters.

matcher.group(1) - get the 1st (only) capturing group.

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

1 Comment

because you have responded quickly may i ask u a question directly ? In this string "ABC Y C S 1 $ 46C M 2/10 S Y FWPIV2". I need create a regex pattern to extract 2/20. All the fields before and space padded to make it fix length. for example ABC till Y is one word then Y C S ... properly space padded. i dont know i have explained it clearly..
4

You should be able to use the substring() method to accomplish this:

string example = "testXXXtest";
string result = example.substring(4,7);

2 Comments

thanks for that. But unfortunately this needs to be added on to an existing framework which uses regex.
@javaagn Such constraints should have been declared in the question.
3

This might help: Groups and capturing in java.util.regex.Pattern.

Here is an example:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Example {
  public static void main(String[] args) {
    String text = "This is a testWithSomeDataInBetweentest.";
    Pattern p = Pattern.compile("test([A-Za-z0-9]*)test");
    Matcher m = p.matcher(text);
    if (m.find()) {
      System.out.println("Matched: " + m.group(1));
    } else {
      System.out.println("No match.");
    }
  }
}

This prints:

Matched: WithSomeDataInBetween

If you don't want to match the entire pattern rather to the input string (rather than to seek a substring that would match), you can use matches() instead of find(). You can continue searching for more matching substrings with subsequent calls with find().

Also, your question did not specify what are admissible characters and length of the string between two "test" strings. I assumed any length is OK including zero and that we seek a substring composed of small and capital letters as well as digits.

Comments

1

You can use substring for this, you don't need a regex.

yourString.substring(4,7);

I'm sure you could use a regex too, but why if you don't need it. Of course you should protect this code against null and strings that are too short.

1 Comment

A regEX pattern is what am looking for. I know it is easy in java as as you and some one else mentioned before.
0

Use the String.replaceAll() Class Method

If you don't need to be performance optimized, you can try the String.replaceAll() class method for a cleaner option:

String sDataLine = "testXXXtest";
String sWhatYouNeed = sDataLine.replaceAll( ".{4}(.{3}).*", "$1" );

References

Comments

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.