splitting text using Pattern.compile - regex

Here is the line of text:
003 STATE BANK OF BIK & JAI A/C.1 2 1,01,500.00 1 3,160.00 98,340.00+
Here is my code snippet to split it:
Pattern pat = Pattern.compile("[ ]");
String strs[] = pat.split(s);
for (int i = 0; i < strs.length; i++) {
System.out.println("Next Token = " + strs[i]);
}
Here is what I get:
003,STATE,BANK,OF,BIK,*,JAI...etc.
What I really want is:
003,STATE BANK OF BIK & JAI,A/C.1,2.1,01,500.00...etc
Which pattern or metacharacter do I use to accomplish this?

For your case this split call will work:
String data = "003 STATE BANK OF BIK & JAI A/C.1 2 1,01,500.00 1 3,160.00 98,340.00+";
String[] arr = data.split(" +(?=\\S*\\d)|(?<![A-Z&]) +");
System.out.println(Arrays.toString(arr));
OUTPUT:
[003, STATE BANK OF BIK & JAI, A/C.1, 2, 1,01,500.00, 1, 3,160.00, 98,340.00+]

Related

Find Replace with RegEx failing for string ending in ? Google script

I have a script in Google sheets
I am trying to find and replace headers on a sheet from a table of values on a different sheet
It is mostly working as desired but the replace is not working for any string that ends in ?
I do not know in advance when a ? will be present
I am using this:
const regex = new RegExp("(?<![^|])(?:" + search_for.join("|") + ")(?![^|])", "g");
I have tried to figure out how to correct my Regex but not getting it
Thanks in advance for your assistance with this
I have in a sheet:
search_for
replace_with
ABC Joe
MNQ
XYZ car
NNN XXX
DDD foo?
Bob bar
I have for Headers on a different sheet:
Label
Id
ABC Joe
XYZ car
DDD foo?
after running the replacement I want for headers:
Label
Id
MNQ
NNN XXX
Bob bar
what I get is:
Label
Id
MNQ
NNN XXX
DDD foo?
var data = range.getValues();
search_for.forEach(function(item, i) {
pair[item] = replace_with[i];
});
const regex = new RegExp("(?<![^|])(?:" + search_for.join("|") + ")(?![^|])", "g");
//Update Header row
//replace(/^\s+|\s+$|\s+(?=\s)/g, "") - Remove all multiple white-spaces and replaces with a single WS & trim
for(var m = 0; m<= data[0].length - 1; m++){
data[0][m] = data[0][m].replace(/^\s+|\s+$|\s+(?=\s)/g, "").replace(regex,(m) => pair[m])
}
A word of warning: what you're doing is scaring me a bit. I hope you know this is a brittle approach and it can go wrong.
You're not quoting the dynamic parts of the regex. The ? is a special character in regular expressions. I've written a solution to your problem below. Don't rely on my solution in production.
//var data = range.getValues();
var data = [
['Label', 'Id', 'ABC Joe', 'XYZ car', 'DDD foo?']
];
var search_for = [
'ABC Joe',
'XYZ car',
'DDD foo?'
];
var replace_with = [
'MNQ',
'NNN XXX',
'Bob bar'
];
var pair = {};
search_for.forEach(function(item, i) {
pair[item] = replace_with[i];
});
const regex = new RegExp("(?<![^|])(?:" + search_for.map((it) => quote(it)).join("|") + ")(?![^|])", "g");
for (var m = 0; m <= data[0].length - 1; m++) {
data[0][m] = data[0][m]
.replace(/^\s+|\s+$|\s+(?=\s)/g, "")
.replace(regex, (m) => pair[m]);
}
// see https://stackoverflow.com/a/3614500/11451
function quote(s) {
var regexpSpecialChars = /([\[\]\^\$\|\(\)\\\+\*\?\{\}\=\!])/gi;
return s.replace(regexpSpecialChars, '\\$1');
}
Can you not do something really simple like escaping all non-alphanumeric characters which would work with the example data you gave above and this seems trustworthy
function quote(s) {
var regexpSpecialChars = /((?=\W))/gi;
return s.replace(regexpSpecialChars, '\\');
}

How do I use reaLline() to find matches in a file using regex and print them out to the console

I am trying to have the user input a class number and name to pull up a list of information on that class I have on a file. I have figured out how to match the information using .toRegex. I can't figure out how to use the users input to find the match they need and not all matching in the file. I am very new to Regnex.
val pattern = """\d+\s+([A-Z]+).\s+(\d+)\s.+\s+\w.+""".toRegex()
val fileName = "src/main/kotlin/Enrollment.txt"
var lines = File(fileName).readLines()// reads every line on the file
do{
print("please enter class name")
var className = readLine()!!
print("please enter class number ")
var classNum = readLine()!!
for(i in 0..(lines.size-1) ){
var matchResult = pattern.find(lines[i])
if(matchResult != null) {
var (className,classNum) = matchResult.groupValues
println("className: $className, class number: $classNum ")
}
}
}while (readLine()!! != "EXIT") ```
example line from file
Name Num
0669 HELP 134 AN CV THING ETC 4.0 4.0 Smith P 001 0173 MTWTh 9:30A 10:30A 23 15 8 4.0
See MatchResult#groupValues reference:
This list has size of groupCount + 1 where groupCount is the count
of groups in the regular expression. Groups are indexed from 1 to
groupCount and group with the index 0 corresponds to the entire
match.
If the group in the regular expression is optional and there were no
match captured by that group, corresponding item in groupValues
is an empty string.
You need
var (_, className,classNum) = matchResult.groupValues
See Kotlin demo:
val lines = "0669 HELP 134 AN CV THING ETC 4.0 4.0 Smith P 001 0173 MTWTh 9:30A 10:30A 23 15 8 4.0 "
val pattern = """^\d+\s+([A-Z]+)\s+(\d+)""".toRegex()
var matchResult = pattern.find(lines)
if(matchResult != null) {
var (_, className,classNum) = matchResult.groupValues
println("className: $className, class number: $classNum ")
}
// => className: HELP, class number: 134
I simplified the regex a bit since find() does not require a full string match to
^\d+\s+([A-Z]+)\s+(\d+)
See the regex demo. Details:
^ - start of string
\d+ - one or more digits
\s+ - one or more whitespaces
([A-Z]+) - Group 1: one or more uppercase ASCII letters
\s+ - one or more whitespaces
(\d+) - Group 2: one or more digits
You need to use a variable in the pattern that you get from the user .readLine()
Use a loop to check each line with another loop checking if the patter is in that line. pattern.containMatchIn()
val className = readLine()!!.toUpperCase()
print("please enter class number ")
val classNum = readLine()!!
val pattern = """\s+\d+\s+$className.\s+$classNum""".toRegex()
for(i in 0..(lines.size-1) ) {
var matchResult = pattern.find(lines[i])
if(matchResult != null ){
if (pattern.containsMatchIn(lines[i])) {
println(lines[i])
}
}
}```

Regex for unknown number of variables that separated by / in express, route

I Have many links like these:
router.use('/foo' , require('./foo'));
router.use('/foo/1' , require('./foo'));
router.use('/foo/2' , require('./foo'));
router.use('/foo/1/1' , require('./foo'));
router.use('/foo/2/3/4/.../n' , require('./foo'));
Is there any way to write a regex that match and separate them by / delimiter? like this:
//for example for /foo/2/3/4/.../n url.
var first_var = req.param[0]; // 2
var last_var = req.param[req.param.length-1]; // n
i already write one for match. not separate:
router.use('/foo/*' , require('./foo'));
As you are trying to do this in regex, this regex will work for you. Let me know, if I've missed some case.
([^\/]+)
const regex = /([^\/]+)/g;
const str = `/foo/2/3/4/5/n`;
let m;
m = str.match(regex);
for(var i = 0; i< 5; i++) {
if(i === 0)
continue;
console.log(`Parameter ${i} = ${m[i]}`);
}

Flash AS3 count capital letters?

How could I count the number of capital letters in a string using flash as3?
for example
var thestring = "This is The String";
should return int 3
Thank you
// Starting string.
var thestring:String = "This is The String";
// Match all capital letters and check the length of the returned match array.
var caps:int = thestring.match(/[A-Z]/g).length;
trace(caps); // 3
One way to solve this is to convert the string to lower case and count the characters affected. That means you don't have to specify which characters to include in the category of "uppercase letters", which isn't trivial. This method supports accented characters such as É.
// Starting string.
var theString:String = "'Ö' is actually the Swedish word for 'island'";
var lowerCase : String = theString.toLowerCase();
var upperCount : int = 0;
for (var i:int = 0; i < theString.length; i++) {
if (theString.charAt(i) != lowerCase.charAt(i)) {
upperCount++;
}
}
trace(upperCount); // prints 2
Each letter in a string has a value that corresponds with that letter:
var myString:String = "azAZ";
trace(myString.charCodeAt(0));
trace(myString.charCodeAt(1));
trace(myString.charCodeAt(2));
trace(myString.charCodeAt(3));
// Output is 97, 122, 65, 90
The name.charCodeAt(x) returns the code of the letter at the position in the string, starting at 0.
From this output we know that a - z are values ranging from 97 to 122, and we also know, that A - Z are values ranging from 65 - 90.
With that, we can now make a For Loop to find capital letters:
var myString:String = "This is The String";
var tally:int = 0;
for (var i:int = 0; i < myString.length; i++)
{
if (myString.charCodeAt(i) >= 65 && myString.charCodeAt(i) <= 95)
{
tally += 1;
}
}
trace(tally);
// Output is 3.
The variable "tally" is used to keep track of the number of capital letters found. In the For Loop, we are seeing if the value of the current letter it is analyzing is between the values 65 and 90. If it is, it adds 1 to tally and then traces the total amount when the For Loop finishes.
Why be succint? I say, processing power is made to be used. So:
const VALUE_0:uint = 0;
const VALUE_1:uint = 1;
var ltrs:String = "This is JUST some random TexT. How many Caps?";
var cnt:int = 0;
for(var i:int = 0; i < ltrs.length; i++){
cnt += processLetter(ltrs.substr(i,1));
}
trace("Total capital letters: " + cnt);
function processLetter(char:String):int{
var asc:int = char.charCodeAt(0);
if(asc >= Keyboard.A && asc <= Keyboard.Z){
return VALUE_1;
}
return VALUE_0;
}
// Heh heh!

Count how many times new line is present?

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;