Scala multiline string extraction using regular expression [duplicate] - regex

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
Given a collection of strings which start all with prefix _abc_ABC( and end with suffix ), for instance,
val a = """_abc_ABC(
{
x = 1
y = 2
})"""
how to define a regular expression that strips out the prefix and suffix above ?

This would work:
val a = """_abc_ABC(
{
x = 1
y = 2
})"""
val Re = """(?s)_abc_ABC\((.*)\)""".r
a match { case Re(content) => println(content) }
The (?s) makes it match over multiple lines.

Related

Reg exp in java for alphabets,numbers and comma [duplicate]

This question already has answers here:
Regular expression for letters, numbers and - _
(6 answers)
Difference between * and + regex
(7 answers)
Closed 4 years ago.
I want a regular expression in java which should allow alphabets,number and comma. Could anyone please help?
I am using the code. But it is not working
Pattern p = Pattern.compile("^[A-Za-z0-9,]$", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(object.toString());
boolean bcheck = m.find();

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");

Regex statement to replace all underscore with space [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I can't use methods like Replace so I need a Regex statement that will replace underscores and add a space instead.
I thought that /([^_])/ would at least return the string without the underscore but it only returns certain strings with the first character.
Sample String x is:
val x = "this_string_contains_Underscore_characters."
Use the below command on this string x:
x.split("_").mkString(" ")
or Use replaceAll:
x.replaceAll("_", " ")
In Scala REPL:
scala> val x = "this_string_contains_Underscore_characters."
x: String = this_string_contains_Underscore_characters.
scala> x.split("_").mkString(" ")
res28: String = this string contains Underscore characters.
scala> x.replaceAll("_", " ")
res50: String = this string contains Underscore characters.

Regex match in string [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I would like to extract two numbers for a strings by regex "[0-9]+"
var str = "ABcDEFG12345DiFKGLSG938SDsFSd"
What I want to extract is "12345" and "938".
But I am not sure how to do so in Kotlin.
This should work:
import java.util.regex.Matcher
import java.util.regex.Pattern
fun main(args:Array<String>) {
val p = Pattern.compile("\\d+")
val m = p.matcher("ABcDEFG12345DiFKGLSG938SDsFSd")
while (m.find())
{
println(m.group())
}
}
Pattern.compile("\\d+"), it will extract the digits from the expression.

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);
}
}
}