2

No matter how much I try to learn RegEx and implement them, I fail.

Below : The first value is the input string, values after colon are the terms I require from the string. The term will always be -> either Input! (A-Z) (number) or Calc! (A-Z) (number)

e.g.Input!A34 or Calc!D93.

Input : Need to replace with some constant/val

  • ADD(Input!A34 + Calc!D93) : Input!A34, Calc!D93
  • Input!D343 = 1000 : Input!D343
  • Calc!D71=IF(HasValue(Input!D4), "FormIs(PartofReturn)", IFERROR(1/0)): Calc!D71,Input!D4

    What pattern should I use for this ?

    My try :

    Pattern findMyPattern = Pattern.compile("(?:Input|" + "Calc!"
                    + ")![a-zA-Z]\\d+");
            Matcher foundAMatch = findMyPattern.matcher(input);
            HashSet hashSet = new HashSet();
            while (foundAMatch.find()) {
                String s = foundAMatch.group(0);
                hashSet.add(s);
            }
    
    
  • 5
    • 1
      I dont understand your question. However "Input![A-Za-z]+\\d+|Calc![A-Za-z]+\\d+" Will this solve your problem? Commented Mar 20, 2013 at 23:42
    • I'm not understanding this either. Can you take another pass at it? I think there's an easy question in here, but your phrasing is convoluted. Commented Mar 20, 2013 at 23:45
    • Yes Maybe. But this problem is solved. Sorry for posting here guys. Commented Mar 20, 2013 at 23:49
    • My mistake. It was an easy problem and I was almost there. Commented Mar 20, 2013 at 23:50
    • (?:Input|Calc)![A-Z][1-9][0-9]* - either Input or Calc followed by ! and capital letter and, finally, the number Commented Sep 9 at 8:17

    2 Answers 2

    1
    Pattern findMyPattern = Pattern.compile("(?:Input|Calc)![A-Z]\\d+");
    
    Sign up to request clarification or add additional context in comments.

    Comments

    0

    Regex

    Pattern p = Pattern.compile("\\b(?:Input|Calc)![A-Z]\\d+\\b");
    

    Breakdown

    1. (?:Input|Calc) — either keyword

    2. ! — the literal exclamation that both forms share

    3. [A-Z] — exactly one uppercase column letter

    4. \\d+ — one or more digits

    5. \\b — word boundaries so you don’t bleed into adjacent text

    Quick test with your samples

    Matches found:

    • ADD(Input!A34 + Calc!D93)Input!A34, Calc!D93

    • Input!D343 = 1000Input!D343

    • Calc!D71=IF(HasValue(Input!D4), "…")Calc!D71, Input!D4

    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.