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 8553763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:43:40+00:00 2026-06-11T14:43:40+00:00

I want to remove all the leading and trailing punctuation in a string. How

  • 0

I want to remove all the leading and trailing punctuation in a string. How can I do this?

Basically, I want to preserve punctuation in between words, and I need to remove all leading and trailing punctuation.

  1. ., @, _, &, /, - are allowed if surrounded by letters
    or digits
  2. \' is allowed if preceded by a letter or digit

I tried

Pattern p = Pattern.compile("(^\\p{Punct})|(\\p{Punct}$)");
Matcher m = p.matcher(term);
boolean a = m.find();
if(a)
    term=term.replaceAll("(^\\p{Punct})", "");

but it didn’t work!!

  • 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-06-11T14:43:41+00:00Added an answer on June 11, 2026 at 2:43 pm

    Ok. So basically you want to find some pattern in your string and act if the pattern in matched.

    Doing this the naiive way would be tedious. The naiive solution could involve something like

    while(myString.StartsWith("." || "," || ";" || ...)
      myString = myString.Substring(1);
    

    If you wanted to do a bit more complex task, it could be even impossible to do the way i mentioned.

    Thats why we use regular expressions. Its a “language” with which you can define a pattern. the computer will be able to say, if a string matches that pattern. To learn about regular expressions, just type it into google. One of the first links: http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial

    As for your problem, you could try this:

    myString.replaceFirst("^[^a-zA-Z]+", "")
    

    The meaning of the regex:

    • the first ^ means that in this pattern, what comes next has to be at
      the start of the string.

    • The [] define the chars. In this case, those are things that are NOT
      (the second ^) letters (a-zA-Z).

    • The + sign means that the thing before it can be repeated and still
      match the regex.

    You can use a similar regex to remove trailing chars.

    myString.replaceAll("[^a-zA-Z]+$", "");
    

    the $ means “at the end of the string”

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

Sidebar

Related Questions

I want to remove all special characters from a string. Allowed characters are A-Z
I want to remove all movieclips in my wrapper movieclip. wrapper.removeMovieClip(ALL CONTENT); how can
I want to remove all non-alphanumeric and space characters from a string. So I
I want to remove all tags except the img tag, $words = strip_tags($words, '<img>');
I want to remove all the unpartnered or unpaired parenthesises from a string. exampleStr
Edit: I solved this by using strip! to remove leading and trailing whitespaces as
Microsoft Doc says about String.Trim Removes all leading and trailing white-space characters from the
I want to remove all files from Git at ~/bin/. I run git rm
I want to remove all vba-modules from an MS Word template using VBScript. I
I want to remove all stored revision info, except for the most up to

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.