how to write a validate function for reading only excel file in xls and xlsx format

how to write a validate function for reading only excel file in xls and xlsx format

How can i write a function to validate if file uploaded is not in excel file format “.xls or .xlsx” it will prompt error message. Any help would be appreciated.
What i’ve tried so far. Below function will prompt error message too if read excel file in .xlsx format .
function verifyFile()
{

if(document.mainform.ATTACH_FILE.value.search(/\.(xls)$/) == -1) {
alert(“Invalid filename extension!”);
return false;
}
}

Expected result : accept both .xls and .xlsx format, prompt messsage if other than these format.

Solutions/Answers:

Solution 1:

You dont need regex to do that.

   var extension = document.mainform.ATTACH_FILE.split(".").pop();

   if (['xls','xlsx'].indexOf(extension) < 0) { 
        alert("Invalid filename extension!");
        return false;
   }

Check other examples in this answer

References

Negate match in RE2 syntax?

Negate match in RE2 syntax?

How can I write a regex in RE2 for “match strings not starting with 4 or 5”?
In PCRE I’d use ^(?!4) but RE2 doesn’t support that syntax.

Solutions/Answers:

Solution 1:

You can use this regex:

^[^45]

^ matches start and [^45] matches anything but 4 or 5 at start.

References

Hive – regexp_replace function for multiple strings

Hive – regexp_replace function for multiple strings

I am using hive 0.13! I want to find multiple tokens like “hip hop” and “rock music” in my data and replace them with “hiphop” and “rockmusic” – basically replace them without white space. I have used the regexp_replace function in hive. Below is my query and it works great for above 2 examples.
drop table vp_hiphop;
create table vp_hiphop as
select userid, ntext,
regexp_replace(regexp_replace(ntext, ‘hip hop’, ‘hiphop’), ‘rock music’, ‘rockmusic’) as ntext1
from vp_nlp_protext_males
;

But I have 100 such bigrams/ngrams and want to be able to do replace efficiently where I just remove the whitespace. I can pattern match the phrase – hip hop and rock music but in the replace I want to simply trim the white spaces. Below is what I tried. I also tried using trim with regexp_replace but it wants the third argument in the regexp_replace function.
drop table vp_hiphop;
create table vp_hiphop as
select userid, ntext,
regexp_replace(ntext, ‘(hip hop)|(rock music)’) as ntext1
from vp_nlp_protext_males
;

Solutions/Answers:

Solution 1:

You can strip all occurrences of a substring from a string using the TRANSLATE function to replace the substring with the empty string. For your query it would become this:

drop table vp_hiphop;
create table vp_hiphop as
select  userid, ntext,
        translate(ntext, ' ', '') as ntext1
from  vp_nlp_protext_males
;

References

Regex For Numbers, Letters, Spaces and Hyphens Only

Regex For Numbers, Letters, Spaces and Hyphens Only

I need a Regex For Numbers, Letters, Spaces and Hyphens Only.
Something like this
^[a-zA-Z0-9]+$
gets letters and numbers but I need one for the above. These are really hard to understand!

Solutions/Answers:

Solution 1:

This is what you need:

/^[0-9A-Za-z\s\-]+$/

References

regex alternates values for #test even though there are no code changes

regex alternates values for #test even though there are no code changes

I’m thoroughly bothered and confused by this. The best way to explain is with the below screen shot.
I’m instantiating the regex object like so:
var PEPPER_STANDARD_DATE_REGEX = /\d{1,2}\/\d{1,2}\/\d{4}, \d{1,2}:\d{1,2} (AM|PM) [A-Z]{1,5}/g

See also: https://www.regex101.com/r/jT2tG4/1

Solutions/Answers:

Solution 1:

This is not bug but a known behavior of g global flag which remembers RegExp.lastIndex position between multiple test or exec calls.

To fix it just remove g flag from your regex i.e.

/\d{1,2}\/\d{1,2}\/\d{4}, \d{1,2}:\d{1,2} (AM|PM) [A-Z]{1,5}/

References

How can I match zero or more instances of a pattern in bash?

How can I match zero or more instances of a pattern in bash?

I’m trying to loop through a bunch of file prefixes looking for a single line matching a given pattern from each file. I have extracted and generalized a couple examples and have used them below to illustrate my question.
I searched for a line that may have some spaces at the beginning, followed by the number 1234, with maybe some more spaces, and then the number 98765. I know the file of interest begins with l76.logsheet and I want to extract the line from the file that ends with one or more numbers. However, I want to make sure I exclude files ending with anything else (of which there are too many options to reasonably use the grep –exclude option). Here’s how I did it from the tcsh shell:
tcsh% grep -E ‘^\s{0,}1234\s+98765’ l76.logsheet[0-9]{0,}
l76.logsheet10:1234 98765 y 13:02:44 2

And here’s another example where I was again searching for 98765, but with a different number out front and a different file prefix:
tcsh% grep -E ‘^\s{0,}4321\s+98765’ k43.logsheet[0-9]{0,}
k43.logsheet1: 4321 98765 y 13:06:38 14

Works great and returns just what I need.
My problem is with the bash shell. Repeating the same command returns a rather interesting result. With the first line, there are no problems:
bash$ grep -E ‘^\s{0,}1234\s+98765’ l76.logsheet[0-9]{0,}

which returns:
l76.logsheet10:1234 98765 y 13:02:44 2

But the result for the second example only has one digit at the end of the filename. This causes bash to throw an error before providing the correct result:
bash$ grep -E ‘^\s{0,}4321\s+98765’ k43.logsheet[0-9]{0,}
grep: k43.logsheet[0-9]0: No such file or directory
k43.logsheet1: 4321 98765 y 13:06:38 14

My question is, how do I search for files ending in zero or more of the previous pattern from the bash shell? I have a work around, but I’m looking for an actual answer to this question, which may save me (and hopefully others) time in the future.

Solutions/Answers:

Solution 1:

First, make sure that extglob is set:

shopt -s extglob

Now, we can match zero or more of any pattern with *(...). For example, let’s create some files and match them:

$ touch logsheet logsheet2 logsheet23 logsheet234
$ echo logsheet*([0-9])
logsheet logsheet2 logsheet23 logsheet234

Documentation

According to man bash, bash offers the following features with extglob:

?(pattern-list)
Matches zero or one occurrence of the given patterns

*(pattern-list)
Matches zero or more occurrences of the given patterns

+(pattern-list)
Matches one or more occurrences of the given patterns

@(pattern-list)
Matches one of the given patterns

!(pattern-list)
Matches anything except one of the given patterns

References

RegEX string to specific lengths

RegEX string to specific lengths

Hey all I am terrible at RegEX so I am posting this question in hopes that a RegEX Guru will easily know and share the answer.
I have the following string types:
508815 AYBK1619RAUEZP
AWBZ4222TYBE1207CWSWER
DEFAULT EP1 O25R60

And I am needing it in this format (split):
508815 AYBK1619 RAU EZP
AWBZ4222 TYBE1207 CWS WER
DEFAULT EP1 O25 R60

So:
xxxxxxxx xxxxxxxx xxx xxx

First 8 characters in string
Next 8 characters in string
Next 3 characters in string
Last 3 characters in string

I can do the Mid(x,x) and all to do that but I figured that using RegEX would be quicker and cleaner looking code.
Any help would be great! Thanks!

Solutions/Answers:

Solution 1:

You can use the following regex to get what you need:

^(\w{0,8})\s*(\w+)\s*(\w{3})(\w{3})$

This regex will:

  1. Match the 0 to 8 word characters from the beginning of the string
  2. Followed by 0 or more spaces
  3. Followed by 1 or more word characters
  4. Followed by 0 or more spaces
  5. Followed by 3 word characters
  6. Followed by 3 word characters
  7. End of string

Word characters (\w) are any alphanumeric character, plus the underscore character. If you strictly want only capital letters for instance, you can replace \w with a character class of A-Z (any letter in the range A-Z), using [A-Z]

See example

Solution 2:

If your desire is to actually use regex to split at those positions, you could use the following:

Dim s As String = "508815  AYBK1619RAUEZP"
Dim m() As String = Regex.Split(s, "(?<=^.{8})|(?<=^.{16})|(?<=^.{19})")
Console.WriteLine(String.Join(" ", m)) '=> "508815   AYBK1619 RAU EZP"

You could also just match the substrings at those positions instead of splitting …

Dim s As String = "AWBZ4222TYBE1207CWSWER"
Dim m As Match = Regex.Match(s, "^(.{8})(.{8})(.{3})(.{3})$")
If m.Success Then
   Console.WriteLine(
          String.Join(" ", 
                      m.Groups(1).Value,
                      m.Groups(2).Value,
                      m.Groups(3).Value,
                      m.Groups(4).Value
                     ))
End If

'**Output => "AWBZ4222 TYBE1207 CWS WER"

References

Java matcher does not find match, even though the regex works separately

Java matcher does not find match, even though the regex works separately

I’m trying to get a ‘teaser’ of a given String and put it as value into a HashMap. With ‘teaser’ I mean a substring (max length 50 characters) ending a word boundary.
Here’s a code sample showing how I’m trying to do it:
import java.util.regex.*;

public class Test {
public static void main(String[] args) throws Exception {
final Pattern pattern = Pattern.compile(“(^.{0,50}\b)”);
final Matcher m = pattern.matcher(
“This is a long string that I want to find a shorter teaser for.”);
if (m.find()) {
System.out.println(“Found: ” + m.group(1));
} else {
System.out.println(“No match”);
}
}
}

I expected it to print:
Found: This is a long string that I want to find a

But instead it prints:
No match

If I test this regex seperately it does what it should – it finds a substring of value which has a max length of 50 characters and ends on word boundary. But if I debug it, m.find always gets me a false.
Any ideas how to solve this? (I’m focused on getting the teaser, not on using Matcher.find() 😉 )

Solutions/Answers:

Solution 1:

According to Oracle documentation on Characters \b is the escape sequence for backspace within a String. However you want \b the regex for word boundary so you need to change the slash to a literal slash, i.e. \\ so that Pattern.compile sees the \b

Pattern.compile("(^.{0,50}\\b)")

You can see this effect by calling .toCharArray() on a String

Single slash

System.out.println(Arrays.toString("\b".toCharArray()));
=> []

Double slash

System.out.println(Arrays.toString("\\b".toCharArray()));
=> [\, b]

References

php regex to match a single letter after digits

php regex to match a single letter after digits

I’ve got the following regex to see if $string contains digits followed by letters. However, I need it to check only that it contains 1 letter after the numeric value. Although the code below works, if $string was to be 28AB then it will also match the regex but it shouldn’t, it should only match any numeric value followed by a single letter.
$string = “28A”;

$containsSpecial = preg_match(‘/[d\^a-zA-Z]/’, $string);

Solutions/Answers:

Solution 1:

^\d+[a-zA-Z]$

Try this.Your code uses [] which can match any character out of the list provided.Also use anchors to make a strict match and no partial matches.See here

References

Selecting columns whose name matches a regular expression in PostgreSQL

Selecting columns whose name matches a regular expression in PostgreSQL

How can I select only those columns whose name matches a regular expression in PostgreSQL?
For example, how do I select only the columns whose name begins with ‘A’ in the following table, without explicitly enumerating them in the select list?
id A1 A2 A3 A4 A5 B
1 a b c d e f
2 g h i j k l

Solutions/Answers:

Solution 1:

You will need to write a dynamic sql(‘select ‘||colname||’ from (yourtable)’) to accomplish this and dynamic sql should have supplied column names from the following sql:

select column_name from information_schema.columns where table_name=(your table) and column_name like ‘a%’;

References