Use grep() to select character strings with "XXX-0000" syntax

Nancy

Given a character vector:

    id.data = c("XXX-2355",
                "XYz-03",
                "XYU-3", 
                "ABC-1234",
                "AX_2356",
                "AbC234")

What is the appropriate way to grep for ONLY the entries that DONT'T follow an "XXX-0000" pattern? In the example above I'd want to end up with only "XXX-2355" and "ABC-1234". There are tens of thousands of records.

I tried selecting by individual issue. For example,

    id.error = rep(NA, length(id.data))
    id.error[-grep("-", id.data)] = "hyphen"

This was obviously really inefficient and I have no way of knowing every possible error. Strplit was useful to a point, but only when I know where to split.

Thanks!

devnull

You seem to be looking for invert:

invert logical. If TRUE return indices or values for elements that do not match.

> id.data = c("XXX-2355",
+                 "XYz-03",
+                 "XYU-3",
+                 "ABC-1234",
+                 "AX_2356",
+                 "AbC234")
> grep("[A-Z]{3}-[0-9]{4}", id.data)
[1] 1 4
> grep("[A-Z]{3}-[0-9]{4}", id.data, value = TRUE)
[1] "XXX-2355" "ABC-1234"
> grep("[A-Z]{3}-[0-9]{4}", id.data, invert = TRUE)
[1] 2 3 5 6
> grep("[A-Z]{3}-[0-9]{4}", id.data, invert = TRUE, value = TRUE)
[1] "XYz-03"  "XYU-3"   "AX_2356" "AbC234"
>

Not sure whether you want strings that match the said pattern, or those that don't match. The above example lists both options.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Grep perl syntax and copyright character

From Dev

Control character in a variable for use with grep

From Dev

using grep to find strings with backslashes - Character Escaping

From Dev

Linux grep only n,m character strings

From Dev

Using grep to select strings contained in a list

From Dev

Grep filenames and use in select menu

From Java

Use grep --exclude/--include syntax to not grep through certain files

From Dev

Use grep to search for multiple strings, but keep order

From Dev

Use grep to find strings with consecutive same letters

From Dev

Use grep to find strings with consecutive same letters

From Dev

Can I use environment variables inside a character range in grep?

From Dev

How to use grep to match lines where the first character falls in a range?

From Dev

MySQL: how to efficiently select strings that have only one character difference?

From Dev

SQL query to select strings that contain a "Unit Separator" character

From Dev

Select text between two strings except with custom escape character

From Dev

Use grep to find either of two strings without changing the order of the lines?

From Dev

Why use an ampersand character (&) in PyQt Strings for GUI elements?

From Dev

Simple SELECT statement fails with "syntax to use near", "ORA-00906", "syntax error at or near" or "syntax near the keyword"

From Dev

How to use awk or grep to select data from different lines?

From Dev

for loop syntax with grep in bash

From Dev

Not working "+" in grep regex syntax

From Dev

for loop syntax with grep in bash

From Dev

R: cannot grep() on "+" character?

From Dev

grep to ignore the first character

From Dev

Grep by character instead on line?

From Dev

R: cannot grep() on "+" character?

From Dev

grep to ignore the first character

From Dev

grep tab and star character

From Dev

Fail to grep by first character

Related Related

  1. 1

    Grep perl syntax and copyright character

  2. 2

    Control character in a variable for use with grep

  3. 3

    using grep to find strings with backslashes - Character Escaping

  4. 4

    Linux grep only n,m character strings

  5. 5

    Using grep to select strings contained in a list

  6. 6

    Grep filenames and use in select menu

  7. 7

    Use grep --exclude/--include syntax to not grep through certain files

  8. 8

    Use grep to search for multiple strings, but keep order

  9. 9

    Use grep to find strings with consecutive same letters

  10. 10

    Use grep to find strings with consecutive same letters

  11. 11

    Can I use environment variables inside a character range in grep?

  12. 12

    How to use grep to match lines where the first character falls in a range?

  13. 13

    MySQL: how to efficiently select strings that have only one character difference?

  14. 14

    SQL query to select strings that contain a "Unit Separator" character

  15. 15

    Select text between two strings except with custom escape character

  16. 16

    Use grep to find either of two strings without changing the order of the lines?

  17. 17

    Why use an ampersand character (&) in PyQt Strings for GUI elements?

  18. 18

    Simple SELECT statement fails with "syntax to use near", "ORA-00906", "syntax error at or near" or "syntax near the keyword"

  19. 19

    How to use awk or grep to select data from different lines?

  20. 20

    for loop syntax with grep in bash

  21. 21

    Not working "+" in grep regex syntax

  22. 22

    for loop syntax with grep in bash

  23. 23

    R: cannot grep() on "+" character?

  24. 24

    grep to ignore the first character

  25. 25

    Grep by character instead on line?

  26. 26

    R: cannot grep() on "+" character?

  27. 27

    grep to ignore the first character

  28. 28

    grep tab and star character

  29. 29

    Fail to grep by first character

HotTag

Archive