javaregex

Regex matching *,à and?


Regex the most difficult one for me.I need the regex for below in Java

1) 17-08-15 11.04A &AD4 USFR EN 00*

My TRY:\d[1-9]+\-\d[1-9]+\-\d[1-9]+\s+\d[1-9]\.\d[1-9]+\w+\s+&+\w+\d\s+\w+\s+\w+\s+\d+

& and *>>Is the first problem and if there is any better regex for the above one please suggest.

2)17-08-15 11.04A &AD4 USER 0002******** j? j? à?&AD4N

PROBLEM>> ******* and ?,&,` how to include these in regex.

I have googled a lot but only simple regex's are available.And the tools are generating all constant type regex taking the strings as it is in reg ex like

16\-09\-14\s+11\.04A\s+&AD4\s+USER\s+EN\s+00

It contains everything as it is so this kind of regex are not useful to me.

Thanks


Solution

  • To search for some special characters (like & and *) you need to escape them by putting \ in front: \*, \&.

    1) Your regex is wrong. As far as I understood first group represents the date, so your regex will work even for such date: 32-99-15. The same applies for time. You can check it here: https://regex101.com/r/tX1iD9/2

    Here is the regex for the date and time part until ampersand 17-08-15 11.04A:

    (0[1-9]|[12]\d|30)-(0[1-9]|1[0-2])-[0-9]{2}\s(0\d|1[1-2])\.([0-5]\d){1,2}(A|P)\s\&
    

    It supports 12-hour time with A or P at the end and proper date, you can test it here: https://regex101.com/r/mF9dQ9/3

    And for the whole string you can find regex here: https://regex101.com/r/mF9dQ9/4

    2) Regex for the second:

    (0[1-9]|[12]\d|30)-(0[1-9]|1[0-2])-[0-9]{2}\s+(0\d|1[1-2])\.([0-5]\d){1,2}(A|P)\s\&[A-Z]{2}\d\s+[A-Z]{4}\s+(\d|\*){12}\s\w\?\s\w\?\s\à\?\&\w{2}\d\w
    

    https://regex101.com/r/eT8eY6/1