0

I am trying to find a regular expression to remove specific letters from the beginning of a string. For example, first occurrence of letter P should be removed like:

P1234 -> 1234
A1234 -> A1234
ABCD -> ABCD
PP5678 -> P5678

I tried to use several expressions, one of them being [^P{1}](.*)$ but everything I tried was removing also the 2nd occurrence of P in PP5678 and I got as result 5678 instead of P5678.

Is there any way to eliminate only first occurrence of letter P?

L.E.: the code that does the replacement looks like this:

    Pattern dataPattern = Pattern.compile(dataFormat);
    Matcher dataMatcher = dataPattern.matcher(barcode);
    if (dataMatcher.find()) {
        fieldValue = dataMatcher.group(0);
        save(fieldValue);
    }

dataFormat is editable in application settings by users, barcode is read by the data scanner.

user3346850
  • 254
  • 4
  • 17
  • 1
    `s.replaceFirst("^P", "")`. Actually, the answer is [here](https://stackoverflow.com/a/14310536/3832970) – Wiktor Stribiżew Nov 13 '19 at 11:36
  • Something like ^([P]).*$ And replace the match with blank. – Ralpharama Nov 13 '19 at 11:44
  • Well @WiktorStribiżew, you had me post again my question by closing it, not nice at all... I cannot use java coding, the code is already written I can only input certain regex that will apply against the data that is read from some barcodes. I can put any type of regex, all worked fine, until I had this one removing all P instead of 1st one. – user3346850 Nov 13 '19 at 12:17
  • 1
    Matching a char/chars at the start of a string is a long solved issue. I added more dupe links. If `^P` does not work, explain what happens and how your issue is unique. Do not repost closed answers, rather edit the current questions explaining why suggested solutions do not work. Right now, [this](https://stackoverflow.com/a/5516151/3832970) is your answer. – Wiktor Stribiżew Nov 13 '19 at 12:18
  • "*I cannot use java coding, the code is already written I can only input certain regex*" please [edit] your question and provide that Java code because correct regex will depend on it. For instance in `replaceAll(regex, target)` regex will need to match part to be removed while something like `Pattern p = Pattern.compile(regex); Matcher m = p.matcher(data); if(m.find()){ result = m.group(); }` would expect regex which matches text which should be *extracted*. In your example you described your task as "*remove*" which suggest matching part which should be removed (like in case of replaceAll). – Pshemo Nov 13 '19 at 13:07
  • Based on your edit now we can assume your regex need to match *remaining* part and not *removable* part. Try with `(?!^P).*` – Pshemo Nov 13 '19 at 13:14
  • Ok, the second version worked fine. Many thanks – user3346850 Nov 13 '19 at 13:18
  • 1
    So, the question is not about removing anything, but about *extracting* all contents after an optional char. Actually, your regex is sort of "fine", the problem is that you placed the limiting quantifier inside a character class, making it all count as literal chars, and that is why not only `P` was skipped, but `{`, `1` and `}`. `"[^P](.*)$"`, `"[^P].*"` will work, too. See [demo](https://ideone.com/2JExBc). I changed the duplicate link to the now relevant one. – Wiktor Stribiżew Nov 13 '19 at 13:29

0 Answers0