Regex match in string [duplicate] - regex

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.

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

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

Scala multiline string extraction using regular expression [duplicate]

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.

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