Stripping specific chars from beginning/ending of a string [duplicate] - java

This question already has answers here:
Regex to trim hyphens from start and end of a string
(2 answers)
Closed 7 years ago.
Given a word-string in Java, I want to strip off from beginning and from end, exactly these specified set of characters:
[?:!.,;'\"«»]
as many times as they appear.
For instance, «Be!!» should become just Be, "Here!!!" should become Here, «I should become I.
Can anyone provide a correct way to do this?

Use an anchored regex in string.replaceAll function.
string.replaceAll("^[?:!.,;'\"«»]+|[?:!.,;'\"«»]+$", "");
DEMO

Related

How to remove alpha characters from timestamp using RegEx? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have timestamps in a field and would like to remove the 'T' and the 'Z' in the value. An example value is 2019-11-01T14:47:43Z. I would like to use a RegEx to solve this problem. I plan to use this in Java.
You can use Java's String.replaceAll() function to remove values with regex. The regular expression [a-zA-Z] will match any one letter; replacing it with an empty string will remove it entirely.
String ts = "2019-11-01T14:47:43Z";
System.out.println(ts.replaceAll("[a-zA-Z]", ""));
2019-11-0114:47:43
Demo

Replacing Regular expression matches in Java [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.

Regular expression to match a word starts and ends with a combination of special characters [duplicate]

This question already has answers here:
Java - Best way to grab ALL Strings between two Strings? (regex?)
(3 answers)
Closed 4 years ago.
I am trying to write a regular expression which gives me words which starts with <!= and ends with =>. For example if there is a sentence what is your <!=name=>, the result should give me name because it matches my pattern.
I have read to use this ^ for starts with and $ for ends with, but I am not able to match a combination of special characters.
As in the comment. You can use <!=(\w+)=> because the exclamation mark and equal sign are not part of word-character class you can simply test for those characters and match the word characters between them. check:https://regex101.com/r/qDrobh/4
For multiple words you can use:<!=((?:\w+| )*)=>
See:https://regex101.com/r/qDrobh/5

Regex too greedy Java [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 7 years ago.
I'm trying to retrieve some parts from a single line html string.
The Regex I wrote is as follows:
String pattern = "rel=\"bookmark\">(.*?)</a></h2>"
The html line is like:
....rel="bookmark">what I need</a></h2>....rel="bookmark">....rel="bookmark">What I also need</a></h2>....
My Regex has two results:
rel="bookmark">what I need</a>
and
rel="bookmark">....rel="bookmark">What I also need</a></h2>
How can I tell it to be less greedy for the second result?
Between tags are the things does not contains < and >
String pattern = "rel=\"bookmark\">[^<>]+</a>";

Java regex for Uk postcodes with spaces [duplicate]

This question already has answers here:
RegEx for matching UK Postcodes
(33 answers)
Closed 7 years ago.
I'm trying to create a regex matching the following patterns (with and without space):
M1 1AA, M60 1NW, CR2 6XH, DN55 1PT, W1A 1HQ and EC1A 1BB
I'm very new at this and find it hard to create a functional regex for all the examples above.
Searching here and there I found a regex that might work for some of the patterns but I don't know how to add the condition "with or without space" for each type of postcode.
Here the regex I found on another post "^(A-PR-UWYZ [0-9][ABD-HJLNP-UW-Z]{2})"
How do I add the space/no space condition? In order to match M11AA or M1 1AA.
You need this regex:
^([A-PR-UWYZ](([0-9](([0-9]|[A-HJKSTUW])?)?)|([A-HK-Y][0-9]([0-9]|[ABEHMNPRVWXY])?)) ?[0-9][ABD-HJLNP-UW-Z]{2})$
^
This space must be set as optional with ? quantifier that means 0 or 1 repetition.
See demo

Categories