Regex - how to get a matched string excluding all groups? [duplicate] - regex

This question already has answers here:
Replace text inside of square brackets
(3 answers)
Closed 5 years ago.
I'm trying to use regex to filter out all parts of a string, enclosed in brackets.
For instance, having input:
data1 data2[comment] data3[comment] data4 data5[comment][comment]
I want to get:
data1 data2 data3 data4 data5
Is it possible to achieve it with regex?

You can use following regex to match everying that's enclosed within brackets (including square brackets):
(\[[^\]]*\])
DEMO: https://regex101.com/r/7HLQkK/1
Java example:
String string = "data1 data2[comment] data3[comment] data4 data5[comment][comment]";
string = string.replaceAll("(\\[[^\\]]*\\])", "");
and javascript:
var string = "data1 data2[comment] data3[comment] data4 data5[comment][comment]";
string = string.replace(/(\[[^\]]*\])/g, "");
console.log(string);

Related

How to replace a string between two string using regex [duplicate]

This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 4 years ago.
How to replace a string between two string in javascript
StartLine = `/*TESTSTART*/`;
Endline = `/*TESTEND*/`;
OriginalContent = `/*TESTSTART*/
testing
not
working
/*TESTEND*/`;
var e = OriginalContent .replace(/(StartLine)[\s\S]*?(Endline)/,' it's
working
fine');
OUTPUT = `/*TESTSTART*/
it's
working
fine
/*TESTEND*/`
1) How to check if the string contains / in regular exp?
2) if I stored sting in one variable, how can I use this variable in regular exp?
You can escape a / character with a backslash \ if you're using / to start your regular expression. But in this case, since you want to include the value of a variable in your regular expression, you should use a string to represent a regex instead, in which case there is no need to escape / but you should escape other special regex characters such as * with two backslashes, and you can simply concatenate the variable with the other string literals and variables to form the full regex:
StartLine = '/\\*TESTSTART\\*/';
Endline = '/\\*TESTEND\\*/';
...
var e = OriginalContent.replace(StartLine + '[\s\S]*?' + Endline, "it's
working
fine");

Remove Bracket [*TESTABC*] along with content from String prefix in java [duplicate]

This question already has answers here:
Java replace all square brackets in a string
(8 answers)
Closed 4 years ago.
I am having string as below and want prefix to be removed [*TESTABC*]
String a = "[*TESTABC*]test#test.com";
Expected result: test#test.com
I tried below code but it is removing the bracklets only. I need to remove the inner content also.
The string with come in this pattern only. Please help.
String s = "[*TESTABC*]test#test.com";
String regex = "\\[|\\]";
s = s.replaceAll(regex, "");
System.out.println(s); //*TESTABC*test#test.com
int index;
String s = "[*TESTABC*]test#test.com";
index=s.indexOf(']');
s=s.substring(index+1);
System.out.println(s);
**i solve your question according to your problem and output **

Python: replace Japanese word with brace, such as {keyword: 部屋}, {keyword: 公園}, ..... with 'keyword' [duplicate]

This question already has answers here:
Match text between two strings with regular expression
(3 answers)
Closed 4 years ago.
there are some Japanese sentences like following:
{keyword: 部屋}いいね!
{keyword: 公園}は綺麗です.
私は{keyword: 部屋捜査}です。
   ..........
.........
I want to replace the substring like :{keyword: 部屋},{keyword: 公園}..... with 'keyword'.
For example:
input: 私は{keyword: 部屋捜査}です
output: 私はkeywordです
My trying code is following and but it is wrong, the result is same:
import re
s = '{keyword: 賃貸}'
t = re.sub(r"\{keyword:[あ-んア-ン一-]+\}", 'keyword', s)
print(t)
Thanks!
Use the following:-
inputString = "私は{keyword: 部屋捜査}です"
t = re.sub(r"\{keyword:[^}]*}", 'keyword', inputString)
print(t)

Getting value using regex [duplicate]

This question already has answers here:
Get values between curly braces c#
(3 answers)
Closed 7 years ago.
how to get the value between first { and last } from a string which have multiple {}.
eg string: ".....[object:{ ..{...{..}...}..}]"
My approach using C#:
line="abcd..efg..[object:{ ab{..c{d.}.e.}f....g}]"
string p = ".*\\[Object:{([A-Za-z{}]*)}\\]";
Regex r = new Regex(p);
Match m=r.match(line);
string value=m.Groups[1].Value.ToString();
Result should be:
value= ab{..c{d.}.e.}f....g
{.*}
or
(?<={).*(?=})
This should do the trick for you.See demo
string strRegex = #"{.*}";
Regex myRegex = new Regex(strRegex, RegexOptions.Multiline);
string strTargetString = #".....[object:{ ..{...{..}...}..}]";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}

Email address validation using regex [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
javascript email validation check condition issue
(2 answers)
Closed 9 years ago.
I am using the email validation as mentioned below :
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#[A-Za- z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
in this pattern i need (hypen, apostrophe, underscore, period) to be included.
For example - pqr.m.o'abc#xyz.com
Please suggest
You can use something like this :
^[_A-Za-z'\\.\\!'0-9-\\+]+(\\.[_A-Za-z0-9\\!\\.'-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})
REGEX: ^([_A-Za-z'\.\!'0-9-\\+]+(\.[_A-Za-z0-9\\!\\.'-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*\.[A-Za-z]{2,})$
Demo
use \. for . where \ is the escape character .
UPDATE
To remove consecutive special characters , you can use :
String ar[] ={ "pqr.m.o''abc#xyz.com","pqr.m.o'abc#xyz.com"};
String REGEX = "[_A-Za-z'\\.\\!'0-9-\\+]+(\\.[_A-Za-z0-9\\!\\.'-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
Pattern p = Pattern.compile(REGEX);
for(String theString:ar){
Matcher m = p.matcher(theString);
while (m.find()) {
String matched = m.group();
String regex = "([._!'-])\\1";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(matched);
if (!matcher.find()) {
System.out.println(matched);
}
}
}