Using regex101 I have developed this regex:
^(\S+)\s_(\S)(\S[^;\s]+)?.*
This works great for 99.999% of the time but occasionally it is run against a string containing a 2 char word that should have matched.
For example it would normally capture...
string _asdf = string.empty;
bool _ttfnow;
//$1 = string
//$2 = a
//$3 = sdf
and
//$1 = bool
//$2 = t
//$3 = tfnow
But for some reason this fails to match the third group?
string _qw = string.empty;
//$1 = string
//$2 = q
//$3 =
Again using regex101 if add add a char it suddenly matches so:
string _qwx = string.empty;
//$1 = string
//$2 = q
//$3 = wx
Any ideas? Thank You
^(\S+)\s_(\S)(\S[^;\s]*)?.*
^^
Just change the quantifier.See demo.
https://regex101.com/r/pG1kU1/33
[^;\s]+ change it to [^;\s]*
/^(\S+)\s_(\S)(\S[^;\s]*)?.*/
Related
code
I want regex for this format and filter out 123456 if match the current format
PO # 123456
PO# 123456
PO #123456
P.O. # 123456
P.O.# 123456
P.O. #123456
We have to filter substring from string notes__c field. my regex is only working for PO #123456 and P.O. #123456
Matcher rm = r.matcher(oOrder.Notes__c);
Pattern r = Pattern.compile('PO #(\\S+)\\s');
if(rm.find()) {
string res2 = rm.group(1);
oOrder.test__c = res2;
}
Try this:
.?(\d+)$
For the example you posted it works.
https://regex101.com/r/eTjbMo/1/
The key thing to note here are the optional whitespace. Whitespace can appear before and/or after the #. Whenever you see a pattern like this, you usually need \s* (if you allow multiple whitespace) or \s? (if you only allow one or more whitespace).
The first part can either be PO or P.O.. We can write these two alternatives with the | token. Remember to escape the .s.
The whole regex looks like this:
(?:PO|P\.O\.)\s*#\s*(\d+)
Here, I assumed the thing you want to capture matches \d+. If that part can contain letters, change the capture group accordingly.
Demo
To write the regex as a string literal, you need to escape the backslashes:
(?:PO|P\\.O\\.)\\s*#\\s*(\\d+)
Above codes are not working friends
i HAVE MODIFIED MY CODE TO
if(oOrder.Notes__c != null) {
Pattern p = Pattern.compile('PO # (\\S+)\\s');
Pattern q = Pattern.compile('PO# (\\S+)\\s');
Pattern r = Pattern.compile('PO #(\\S+)\\s');
Pattern s = Pattern.compile('P.O. # (\\S+)\\s');
Pattern t = Pattern.compile('P.O.# (\\S+)\\s');
Pattern u = Pattern.compile('P.O. #(\\S+)\\s');
Matcher pm = p.matcher(oOrder.Notes__c);
Matcher qm = q.matcher(oOrder.Notes__c);
Matcher rm = r.matcher(oOrder.Notes__c);
Matcher sm = s.matcher(oOrder.Notes__c);
Matcher tm = t.matcher(oOrder.Notes__c);
Matcher um = u.matcher(oOrder.Notes__c);
system.debug('find = ' +pm.find());
if (pm.find()){
string res = pm.group(1);
oOrder.test__c = res;
}
if(qm.find()){
string res1 = qm.group(1);
oOrder.test__c = res1;
}
if(rm.find()){
string res2 = rm.group(1);
oOrder.test__c = res2;
}
if(sm.find()){
string res3 = sm.group(1);
oOrder.test__c = res3;
}
if(tm.find()){
string res4 = tm.group(1);
oOrder.test__c = res4;
}
if(um.find()){
string res5 = um.group(1);
oOrder.test__c = res5;
}
}
it is covering everything except First one
PO # 123456
I want to taking every string who is located on a new line with Regular Expression
string someStr = "first
second
third
"
example:
string str1 = "first";
string str2 = "second";
string str3 = "third";
Or if you just want the first word of each line;
^(\w+).*$ with multi-line flag.
Regex101 has a nice regex testing tool: https://regex101.com/r/JF3cKR/1
Just split it with "\n";
someStr.split("\n")
And you can filter the empty strings if you'd like
Or if you really want regex, do /^.*$/ with multiline flag
List<String> listOfLines = new ArrayList<String>();
Pattern pattern = Pattern.compile("^.*$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher("first\nsecond\nthird\n");
while (matcher.find()) {
listOfLines.add(matcher.group());
}
Then you have;
listOfLines.get(0) = first
listOfLines.get(1) = second
listOfLines.get(2) = third
You can use the following regex :
(\w+)(?=\n|"|$)
see demo
My string:
null[00:14.04]I've /n[00:14.11]got /n[00:14.18]a /n[00:14.25]fee- /n[00:15.02]ling /n
I am trying to obtain every data between [<--->] brackets. Here's my code.
String find = "[(.*?)\\\\]";
Pattern patern = Pattern.compile(find);
Matcher matcher = patern.matcher(intake);
while(matcher.find()){
i++;
matcher.find(i);
int start = matcher.start();
int end = matcher.end();
String group = matcher.group();
}
The first results are:
start = 10
end = 11
group = "."
What I wanted was (Counting on my head)
start = 4
end = 14
group = [00:14.04]
Next is
start = 22
end = 32
group = [00:14.11]
and so on
What is the correct pattern?
You're using wrong escaping. Use this regex:
String find = "\\[(.*?)\\]";
EDIT: Based on your comment:
If you want to capture all items inside square brackets just run your while loop like this:
while(matcher.find()) {
String matched = matcher.group(1);
System.out.printf("Matched Group: [%s]%n", matched);
}
I want to get two decimals from a string using a Regex but I get only the first.
getGroupCount is correct but I get always {1} and I don't know why. I'm using GWT 2.5. Here is my code:
private void readOffset(){
RegExp regExp = RegExp.compile("(\\{\\d\\})");
MatchResult matcher = regExp.exec("(cast({1} as float)/{24})");
String val1 = matcher.getGroup(0);
String val2 = matcher.getGroup(1);
}
Why might this be happening?
The operator \d will only yield 1 digit. If you want to get two, you would need to use \d{2}. If you need to match more, you would need to use \d+, where + means 1 or more repetitions of.
Something like so worked for me (Java though, not exactly GWT):
String str = "(cast({1} as float)/{24})";
Pattern p = Pattern.compile("(\\{\\d+\\})");
Matcher m = p.matcher(str);
while(m.find())
{
System.out.println(m.group(1));
}
Yields: {1} and {24}
To learn how to use Regex in GWT client side please go through the Unit test cases in GWT for Regex. Reference - GWT Unit Test for Regex
Also you should be using RegExp and MatchResult from com.google.gwt.regexp.shared.
im to stupid for the regex so i solved it quick and dirty:
private void readOffset(){
String offset = manager.get("offset");
String v1 = offset.substring(offset.indexOf("{")+1, offset.indexOf("}"));
String v2 = offset.substring(offset.lastIndexOf("{")+1, offset.lastIndexOf("}"));
multiplikator.setValue(v1);
divisor.setValue(v2);
/*
RegExp regExp = RegExp.compile(".*({\\d+}).*", "g");
MatchResult matcher = regExp.exec(offset);
boolean matchFound = (matcher != null);
if(matchFound == true && matcher.getGroupCount() == 2){
String val1 = matcher.getGroup(0);
String val2 = matcher.getGroup(1);
multiplikator.setValue(matcher.getGroup(0));
divisor.setValue(matcher.getGroup(1));
}else{
multiplikator.setValue("1");
divisor.setValue("1");
}
*/
}
better solutions are welcome :(
For example,
string="help/nsomething/ncrayons"
Output:
String word count is: 3
This is what I have but the program is looping though the method several times and it looks like I am only getting the last string created. Here's the code block:
Regex regx = new Regex(#"\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
MatchCollection matches = regx.Matches(output);
//int counte = 0;
foreach (Match match in matches)
{
//counte = counte + 1;
links = links + match.Value + '\n';
if (links != null)
{
string myString = links;
string[] words = Regex.Split(myString, #"\n");
word_count.Text = words.Length.ToString();
}
}
It is \n for newline.
Not sure if regex is a must for your case but you could use split:
string myString = "help/nsomething/ncrayons";
string[] separator = new string[] { "/n" };
string[] result = myString.Split(separator, StringSplitOptions.None);
MessageBox.Show(result.Count().ToString());
Another way using regex:
string myString = "help/nsomething/ncrayons";
string[] words = Regex.Split(myString, #"/n");
word_count.Text = words.Length;