Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:07:01+00:00 2026-05-26T13:07:01+00:00

All groovy special character #{\’}${}/’, needs to be replaced by \ in front in

  • 0

All groovy special character #{\’}${“}/’, needs to be replaced by \ in front in a groovy string dynamically

input  : anish$spe{cial
output : anish\$spe\{cial
input  : anish}stack{overflow'
output : anish\}stack\{overflow\'

I have written a sample program in Java, that i want in groovier way

import java.util.regex.*;
import java.io.*;

/**
 * 
 * @author anish
 *
 */
public class EscapeSpecialChar {
    public static void main(String[] args) throws IOException {
        inputString();
    }
    private static void inputString() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter string to find special characters: ");
        String string = in.readLine();
        // Escape the pattern
        string = escapeRE(string);  
        System.out.println("output: -- " + string);
    }

    // Returns a pattern where all punctuation characters are escaped.
    static Pattern escaper = Pattern.compile("([^a-zA-z0-9])");
    public static String escapeRE(String str) {
        return escaper.matcher(str).replaceAll("\\\\$1");
    }
}

Enter string to find special characters: $Anish(Stack%1231+#$124{}

output: — \$Anish\(Stack\%1231\+\#\$124\{\}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:07:01+00:00Added an answer on May 26, 2026 at 1:07 pm

    This does what your Java code does:

    System.console().with {
      def inStr = readLine 'Enter string to find special characters: '
      def outStr = inStr.replaceAll( /([^a-zA-Z0-9])/, '\\\\$1' )
      println "Output: $outStr"
    }
    

    I am still dubious that what I think you are doing is a good idea though… 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a String, I know Groovy provides convenience methods like String.findAll(String, Closure) Finds all
I've been using Groovy for all of five hours and just came across the
I'm doing some research on the Groovy programming language, and despite all the information
All of my tests for my Groovy code look like this public void testButtons()
Is there a way to parse all all the classes in a groovy script?
The following Groovy commands illustrate my problem. First of all, this works ( as
On a Mac with JDK and all Groovy necessities installed, how can Groovy files
All variables seem to be global in my groovy scripts I run on groovy
With all the shorthand ways of doing things in Groovy, there's got to be
I am writing a Groovy script that needs to POST JSON to a URL.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.