开发者

Pattern Matching - String Search

I am trying to work out a formula to match a following pattern: input string example:

'444'/'443'/'434'/'433'/'344'/'334'/'333'

if any of the patterns above exist in a particular input string I want to match it as the same pattern.

also is it possible to do a variable substitution using regex? meaning check for the 3 chars of the string by using each character as a variable and just doing an increment/decrement for each character? so that you dont have to specify the particular number ranges (hardcoding the pattern string ) for different patterns?

Is there any good library one can use for this?? I was working with Pattern class in java.

开发者_Python百科

If you have any link which would be helpful please pass it through :) Thank you.


Let's first consider this pattern: [34]{3}

The […] is a character class, it matches exactly one of the characters in the set. The {n} is an exact finite repetition.

So, [34]{3} informally means "exactly 3 of either '3' or '4'". Thus, it matches "333", "334", "343", "344", "433", "434", "443", "444", and nothing else.

As a string literal, the pattern is "[34]{3}". If you don't want to hardcode this pattern, then just generate similar-looking strings that follows this template "[…]{n}". Just put the characters that you want to match in the , and substitute n with the number you want.

Here's an example:

  String alpha = "aeiou";
  int n = 5;

  String pattern = String.format("[%s]{%s}", alpha, n);

  System.out.println(pattern);
  // [aeiou]{5}

We've now seen that the pattern is not hardcoded, but rather programmatically generated depending on the values of the variables alpha and n. The pattern [aeiou]{5} will 5 consecutive lowercase vowels, e.g. "ooiae", "ioauu", "eeeee", etc.

It's again not clear if you just want to match these kinds of strings, or if they have to appear like '…'/'…'/'…'/'…'/'…'. If the latter is desired, then simply compose the pattern as desired, using repetition and grouping as necessary. You can also just programmatically copy and paste the pattern 5 times if that's simpler. Here's an example:

  String p5 = String.format("'%s'/'%<s'/'%<s'/'%<s'/'%<s'", pattern);

  System.out.println(p5);
  // '[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'

This will now match strings like "'aeooi'/'eeiuu'/'uaooo'/'eeeia'/'eieio'".

Caveat

Do be careful about what goes in alpha. Specifically, -, [. ], &&, ^, etc, are special metacharacters in Java character class definition. If you restrict alpha to contain only digits/letters, then you will probably not run into any problems, but e.g. [^a] does NOT mean "either '^' or 'a'". It in fact means "anything but 'a'. See java.util.regex.Pattern for exact character class syntax.


You can use the regex:

('\\d{3}'/){6}'\\d{3}'


Pattern.Compile takes a String as its parameter. Though that's probably most often supplied in the form of a string literal, if you have variable upper and lower bounds for your pattern, you can use something like StringBuilder to build your string, then pass that result to Pattern.Compile.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜