-3

is it possible to detect the pattern of a String and store it in a variable? so, if I have a String test1234 and highlight 1234 I expect something like \d{4}.

user
  • 157
  • 2
  • 14
  • pattern is used to detect a particular substring. – Avinash Raj Feb 20 '15 at 12:31
  • in some extends you could do it, but what about if you have single number, ie `1` will your pattern will be `\d{1}` or maybe just `1` – user902383 Feb 20 '15 at 12:40
  • the idea is, i highlight a part of a string or text and replace that, but it has to be in the same pattern. so, if i highlight 1234 it shouldn't be possible to replace it with a123, but 9999 would be ok. – user Feb 20 '15 at 12:46

1 Answers1

0

It would require that you find a regular expression that both your highlighted substring and desired replacement match and that is in no way unique. For example, "1234" could match .{4} or \d{4} or even .+ , which is not of a unique length. So, even if you could generate a regular expression from a string, it could happen that it would be the string itself or something you didn't want. Maybe you should rethink the general desired outcome of your program and try to come up with a different way of solving the issue at hand. Hope that helped. Good luck!

janisz
  • 6,292
  • 4
  • 37
  • 70
reaponja
  • 1
  • 2